創(chuàng)建對(duì)象的幾種方式

真是百看不如一練啊

/*var box= new Object(); //創(chuàng)建一個(gè)對(duì)象

box.name= "xiaoju";? //給對(duì)象添加一個(gè)屬性

box.age = 100;

box.run = function(){? //定義一個(gè)方法

return this.name + this.age + "運(yùn)行中...";

}

var box1 = new Object();

box1.name = "nanlan";

box1.age = 200;

box1.run = function(){

return this.name +this.age;

}*/

/*alert(box.run());*/


//object構(gòu)造函數(shù)創(chuàng)建多個(gè)對(duì)象,容易造成代碼冗余

var box= new Object(); //創(chuàng)建一個(gè)對(duì)象

box.name= "xiaoju";? //給對(duì)象添加一個(gè)屬性

box.age = 100;

box.run = function(){? //定義一個(gè)方法

return this.name + this.age + "運(yùn)行中...";

}

var box1 = box;

box1.name = "nanlan";

box1.age = 200;

box1.run = function(){

return this.name +this.age;

}

alert(box.run());

alert(boxbdnare 1.run());

*/


//工廠(chǎng)模式,用函數(shù)封裝特定接口創(chuàng)建對(duì)象的細(xì)節(jié)。工廠(chǎng)模式雖然解決了多個(gè)對(duì)象相似的問(wèn)題,但是并沒(méi)有解決對(duì)象識(shí)別的問(wèn)題

function createObject(name,age){

varobj =newObject();

obj.name = name;

obj.age = age;

obj.run =function(){

return this.name +this.age +"運(yùn)行中";

};//方法必須加上分號(hào)

returnobj;//返回對(duì)象引用,在工廠(chǎng)模式當(dāng)中,必須有返回語(yǔ)句

}//函數(shù)可以不用

function createObject2(name,age){

varobj =newObject();

obj.name = name;

obj.age = age;

obj.run =function(){

return this.name +this.age +"運(yùn)行中";

};//方法必須加上分號(hào)

return obj;//返回對(duì)象引用,在工廠(chǎng)模式當(dāng)中,必須有返回語(yǔ)句

} //函數(shù)可以不用加分號(hào)

varbox =? createObject("nanlan",20);

varbox1 =? createObject("xiaoju",21);

varbox2 = createObject("yunju",22);

/*

alert(box.run());

alert(box1.run());*/

alert(boxinstanceofObject);

alert(box1instanceofObject);

alert(box2instanceofObject);

//這三個(gè)提示框都是ture,可是怎么它們是誰(shuí)的對(duì)象呢,box 、box1是createObject的對(duì)象,box2是createObject2的對(duì)象,所以有了以下的方式——構(gòu)造函數(shù)模式


//構(gòu)造函數(shù)模式,解決了Object多個(gè)實(shí)例的問(wèn)題和工廠(chǎng)模式對(duì)象識(shí)別的問(wèn)題

function? Box(user,age){

this.user = user;

this.age = age;

this.run = run;

}

function? Desk(user,age){

this.user = user;

this.age = age;

this.run = function(){

return this.name + this.age;

}

}

function run(){ ? //把構(gòu)造內(nèi)部的方法通過(guò)全局實(shí)現(xiàn)

return this.user + this.age;

}

//構(gòu)造函數(shù)沒(méi)有new Object() ,但它后臺(tái)自動(dòng) var obj = new Object;

//必須使用new構(gòu)造函數(shù)名,new Box(),Box第一個(gè)字母也是大寫(xiě)的

//必須使用new 運(yùn)算符

var box1 = new Box("nanlan",20);

var box2 = new Box("xiaoju",21);

alert( box1 instanceof? Object);?

alert( typeof box2);

alert(box1 instanceof? Box);

alert(box2 instanceof? Box);

alert(box3 instanceof? Box); //構(gòu)造函數(shù)能夠彌補(bǔ)工廠(chǎng)模式的缺點(diǎn),即對(duì)象識(shí)別的問(wèn)題,這句代碼已經(jīng)得到體現(xiàn),這三個(gè)提示框的輸出結(jié)果是ture,ture,false

var o = {};? //對(duì)象冒充

Box.call(o,"juju",20);

alert(o.run());*/

alert(box1.run() == box2.run()); //結(jié)果是false,因?yàn)樗麄儽容^的是地址


原型模式

function Box(){}

Box.prototype = {

constructor:Box,

name:'Lee',

age:100,

family:['哥哥','姐姐','妹妹'],

run: function(){

return this.name + this.age +"運(yùn)行中"

}

};

var box1 = new Box();

/!*alert(box1.family);*!/

//box1.family=["蘋(píng)果","香蕉","梨子"];

box1.family.push("蘋(píng)果","香蕉","梨子");

box1.name="xiaoju";

alert(box1.family);

var box2 = new Box();

//alert( box2.family); //結(jié)果是 哥哥、姐姐、妹妹

alert(box2.family); // 結(jié)果是哥哥、姐姐、妹妹,蘋(píng)果,香蕉,梨子

*/

//我的天那,這是原型模式的一個(gè)缺點(diǎn)。按道理來(lái)說(shuō)實(shí)例是不能修改原型當(dāng)中的實(shí)例屬性和方法的,但是

//很明顯在這里被修改了。原因是原型中所有屬性是很多實(shí)例共享的,共享對(duì)于函數(shù)非常合適,對(duì)于包含基本類(lèi)型的

//也還可以,但是如果屬性包含引用類(lèi)型,就存在一定的問(wèn)題。解決辦法是通過(guò)組合構(gòu)造函數(shù)+原型模式去寫(xiě)


/*/組合構(gòu)造函數(shù) + 原型模式*/

function Box(name,age){? //保持獨(dú)立的用構(gòu)造函數(shù)

this.name = name;

this.age = age;

this.family = ["哥哥","姐姐","妹妹"];

}

Box.prototype = {? //保持共享的用原型

constructor: Box,

run:function(){

return this.name + this.age;

}

}

var box1 = new Box('nanlan',20);

box1.family.push('弟弟');

alert(box1.family);

var box2 = new Box("xiaoju",21);

alert(box2.family); //結(jié)果是哥哥、姐姐、妹妹


/*//寄生構(gòu)造函數(shù) = 工廠(chǎng)模式+構(gòu)造函數(shù)模式

function Box(name,age){

var obj = new Object();

obj.name = name;

obj.age = age;

obj.run = function(){

return this.name + this.age;

}

return obj;

}

var box1 = new Box('Lee',100);

alert(box1.run());

var box2 = new Box('Jack',200);

alert(box2.run());*/


//穩(wěn)妥構(gòu)造函數(shù)

function Box(name,age){

var obj =new Object();

obj.name = name;

obj.age = age;

obj.run = function(){

return this.name +this.age;

}

return obj;

}

varbox1 = Box('nanlan',200);

alert(box1.run());

varbox2 = Box('xiaoju',100);

alert(box2.run());

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容