Janeiro 9, 2008
Como criar uma classe em javascript:
Definir a classe InputText
/* definição dos tipos */
function InputText(id) // constructor da classe
{
this.id = id; // campos da classe
this.text = ‘jkjh’; // campos da classe
}
// definição das operações através da propriedade prototype de cada classe
InputText.prototype.setTextInInput = function () {
var i = document.getElementById(this.id); // o this indica a instância do objecto que está a invocar a operação
if (i)
{
i.value = this.text;
}
}
InputText.prototype.getTextInInput = function () {
var i = document.getElementById(this.id);
if (i)
{
return i.value;
}
}
InputText.prototype.getText = function () {
this.text = this.getTextInInput();
return this.text;
}
InputText.prototype.setText = function (text) {
this.text = text;
alert(’a:’ + text);
this.setTextInInput();
}
InputText.prototype.clear = function () {
this.text = ”;
this.setTextInInput();
}
Como usar:
var itext = new InputText(’itext’);
itext.setText(’SLB’);