| 導購 | 订阅 | 在线投稿
分享
 
 
 

Javascript對象定義的幾種方式

來源:互聯網網民  2008-09-09 07:29:21  評論

一.工廠方式:先創建對象,再向對象添加方法和屬性,封閉後調用不要使用new操作符創建對象。使用這種方法有很多弊端,把方法定義在工廠函數內部的時候,每次調用的時候都會産生新的函數

function factory(name,person,address,time){

var tmp=new Object;

tmp.name=name;

tmp.person=person;

tmp.address=address;

tmp.workTime=function(){

alert("we start to work at" + time);

}

return tmp;

}

var factory1=factory("drugs",100,"huashan Rd",10);

var factory2=factory("TCMdrugs",100,"hongqiao Rd",11);

factory1.workTime();

factory2.workTime();//Here,factory1 and factory2 have different method

對這種問題雖然可以用下面的方式改進,但是缺乏很好的封裝性

function factory(name,person,address,time){

var tmp=new Object;

tmp.name=name;

tmp.person=person;

tmp.address=address;

tmp.workTime=workTime();

return tmp;

}

function workTime(){

alert("we start to work at" + this.time);

}

二,構造函數方式,在構造函數內部不創建對象,使用this關鍵字,使用時候用new操作符,存在和工廠方式相同的問題,重複創建函數。

function counstruct(name,person,address,time){

this.name=name;

this.person=person;

this.address=address;

this.workTime=function(){

alert("we start to work at" + this.time);

};

}

三.原型方式:利用prototype屬性來實現屬性和方法,可以通過instanceof 檢查對象類型,解決了重複創建函數的問題,但不能通過傳遞參數初始化屬性

function Car(){

}

Car.prototype.color = "red";

Car.prototype.doors = 4;

Car.prototype.mpg = 23;

Car.prototype.showColor = function(){

alert(this.color);

};

var car1 = new Car();

var car2 = new Car();

但是如果遇到下面的情況,又出問題了

Car.prototype.drivers = new Array("mike", "sue");

car1.drivers.push("matt");

alert(car1.drivers); //outputs "mike,sue,matt"

alert(car2.drivers); //outputs "mike,sue,matt"

drivers是指向Array對象的指針,Car的兩個實例都指向同一個數組。

四.混合的構造函數/原型方式:針對原型方式的解決方案

function Car(sColor, iDoors, iMpg){

this.color = sColor;

this.doors = iDoors;

this.mpg = iMpg;

this.drivers = new Array("mike", "sue");

}

Car.prototype.showColor = function (){

alert(this.color);

};

var car1 = new Car("red", 4, 23);

var car2 = new Car("blue", 3, 25);

car1.drivers.push("matt");

alert(car1.drivers);

alert(car2.drivers);

五.動態原型方式:這種方式是極力推薦的方式,避免了前面幾種方式所出現的問題,提供了更友好的編碼風格

function Car(sColor, iDoors, iMpg){

this.color = sColor;

this.doors = iDoors;

this.mpg = iMpg;

this.drivers = new Array("mike", "sue");

if(typeof Car.initialized == "undefined"){

Car.prototype.showColor = function (){

alert(this.color);

};

Car.initialized = true;

}

}

var car1 = new Car("red", 4, 23);

var car2 = new Car("blue", 3, 25);

car1.drivers.push("matt");

alert(car1.drivers);

alert(car2.drivers);

六.混合工廠方式:和工廠方式有些相似,但采用new關鍵字實例化,具有和工廠方式相同的弊端,不推薦使用

 
特别声明:以上内容(如有图片或视频亦包括在内)为网络用户发布,本站仅提供信息存储服务。
 
一.工廠方式:先創建對象,再向對象添加方法和屬性,封閉後調用不要使用new操作符創建對象。使用這種方法有很多弊端,把方法定義在工廠函數內部的時候,每次調用的時候都會産生新的函數 function factory(name,person,address,time){ var tmp=new Object; tmp.name=name; tmp.person=person; tmp.address=address; tmp.workTime=function(){ alert("we start to work at" + time); } return tmp; } var factory1=factory("drugs",100,"huashan Rd",10); var factory2=factory("TCMdrugs",100,"hongqiao Rd",11); factory1.workTime(); factory2.workTime();//Here,factory1 and factory2 have different method 對這種問題雖然可以用下面的方式改進,但是缺乏很好的封裝性 function factory(name,person,address,time){ var tmp=new Object; tmp.name=name; tmp.person=person; tmp.address=address; tmp.workTime=workTime(); return tmp; } function workTime(){ alert("we start to work at" + this.time); } 二,構造函數方式,在構造函數內部不創建對象,使用this關鍵字,使用時候用new操作符,存在和工廠方式相同的問題,重複創建函數。 function counstruct(name,person,address,time){ this.name=name; this.person=person; this.address=address; this.workTime=function(){ alert("we start to work at" + this.time); }; } 三.原型方式:利用prototype屬性來實現屬性和方法,可以通過instanceof 檢查對象類型,解決了重複創建函數的問題,但不能通過傳遞參數初始化屬性 function Car(){ } Car.prototype.color = "red"; Car.prototype.doors = 4; Car.prototype.mpg = 23; Car.prototype.showColor = function(){ alert(this.color); }; var car1 = new Car(); var car2 = new Car(); 但是如果遇到下面的情況,又出問題了 Car.prototype.drivers = new Array("mike", "sue"); car1.drivers.push("matt"); alert(car1.drivers); //outputs "mike,sue,matt" alert(car2.drivers); //outputs "mike,sue,matt" drivers是指向Array對象的指針,Car的兩個實例都指向同一個數組。 四.混合的構造函數/原型方式:針對原型方式的解決方案 function Car(sColor, iDoors, iMpg){ this.color = sColor; this.doors = iDoors; this.mpg = iMpg; this.drivers = new Array("mike", "sue"); } Car.prototype.showColor = function (){ alert(this.color); }; var car1 = new Car("red", 4, 23); var car2 = new Car("blue", 3, 25); car1.drivers.push("matt"); alert(car1.drivers); alert(car2.drivers); 五.動態原型方式:這種方式是極力推薦的方式,避免了前面幾種方式所出現的問題,提供了更友好的編碼風格 function Car(sColor, iDoors, iMpg){ this.color = sColor; this.doors = iDoors; this.mpg = iMpg; this.drivers = new Array("mike", "sue"); if(typeof Car.initialized == "undefined"){ Car.prototype.showColor = function (){ alert(this.color); }; Car.initialized = true; } } var car1 = new Car("red", 4, 23); var car2 = new Car("blue", 3, 25); car1.drivers.push("matt"); alert(car1.drivers); alert(car2.drivers); 六.混合工廠方式:和工廠方式有些相似,但采用new關鍵字實例化,具有和工廠方式相同的弊端,不推薦使用
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 
 熱帖排行
 
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有