2009/05/26

自訂物件@Javascript

每次重頭開始寫javascript物件,都會忘了怎麼去寫 constructor。今天把範例放上來,以免以後又忘。

function test(width){
var myObj = new azObject(width) ;
alert(myObj.width);
myObj.enlarge(2);
alert(myObj.width);
}

function azObject(_width){
this.width = _width ;
this.enlarge = azEnlarge ;
}

function azEnlarge(plus){
this.width = this.width * plus ;
}