1逃顶、工廠模式創(chuàng)建對(duì)象
//外部創(chuàng)建
// function Car(model,color){
// this.model = model;
// this.color = color;
// }
function factory(model,color){
//內(nèi)部創(chuàng)建
function Car(model,color){
this.model = model;
this.color = color;
}
var o = new Car(model,color);
return o;
}
使用工廠模式能夠創(chuàng)建一個(gè)包含所有信息的對(duì)象讨便,可以無數(shù)次的調(diào)用的這個(gè)函數(shù)。雖然其解決了創(chuàng)建多個(gè)相似對(duì)象的問題以政,但卻沒有解決對(duì)象識(shí)別的問題(即如何得知一個(gè)對(duì)象的類型)
2霸褒、構(gòu)造函數(shù)創(chuàng)建對(duì)象
function Car(model,color){
this.model = model;
this.color = color;
this.run = function(){
console.log(this.model + "跑~");
}
}
var c1 = new Car("x1","blue");
var c2 = new Car("x2","red");
c1.run();
c2.run();
console.log("構(gòu)造函數(shù)"+c1.run == c2.run);
//false表示兩個(gè)不同的函數(shù)對(duì)象
- 構(gòu)造函數(shù)創(chuàng)建對(duì)象缺點(diǎn):
函數(shù)是對(duì)象,c1,c2是不同的兩個(gè)對(duì)象盈蛮,每次new都會(huì)創(chuàng)建一個(gè)對(duì)象废菱,并且每個(gè)方法都有在每個(gè)實(shí)例上重新創(chuàng)建一遍,占內(nèi)存占空間
- 解決方法:原型方式創(chuàng)建對(duì)象
注意:構(gòu)造函數(shù)其實(shí)和普通的函數(shù)沒有太大的差別抖誉,唯一的不同在于調(diào)用方式的不同殊轴。
3、原型創(chuàng)建對(duì)象
function Apple(name){
this.name = name;
}
Apple.prototype.eat = function(){
console.log(this.name + "被吃了");
}
var a1 = new Apple("紅蘋果");
var a2 = new Apple("青蘋果");
a1.eat();
a2.eat();
console.log("原型:"+a1.eat == a2.eat);
//true兩個(gè)對(duì)象指向同一個(gè)內(nèi)存區(qū)域袒炉,省內(nèi)存
原型模式也不是沒有缺點(diǎn)旁理,首先,它省略了構(gòu)造函數(shù)傳遞初始化參數(shù)這一環(huán)節(jié)我磁,結(jié)果所有實(shí)例在默認(rèn)情況下都取得了相同的屬性值孽文,這樣非常不方便驻襟,但這還是不是原型的最大問題,原型模式的最大問題在于共享的本性所導(dǎo)致的芋哭,由于共享沉衣,因此因此一個(gè)實(shí)例修改了引用,另一個(gè)也隨之更改了引用减牺。因此我們通常不單獨(dú)使用原型豌习,而是結(jié)合原型模式與構(gòu)造函數(shù)模式。
4拔疚、工廠模式+字面量創(chuàng)建對(duì)象
//var obj = {} 等同于 var obj1 = new Object();
function ObjectFactory(m,s){
var o = new Object();
o.model = m;
o.size = s;
return o;
}
var c1 = ObjectFactory("t420",14);
var c2 = ObjectFactory("M920",15);
var c3 = ObjectFactory("G520",10);
var c4 = ObjectFactory("K496",13);
console.log(c1 == c2);//false
5斑鸦、混合模式(原型模式 + 構(gòu)造函數(shù)模式)
function Blog(name, url, friend) {
this.name = name;
this.url = url;
this.friend = friend;
}
Blog.prototype.alertInfo = function() {
alert(this.name + this.url + this.friend);
}
var blog = new Blog('wuyuchang', 'http://tools.jb51.net/', ['fr1', 'fr2', 'fr3']);
var blog2 = new Blog('wyc', 'http://**.com', ['a', 'b']);
blog.friend.pop();
blog.alertInfo();
// wuyuchanghttp://tools.jb51.net/fr1,fr2
blog2.alertInfo();
// wychttp://**.coma,b
混合模式中構(gòu)造函數(shù)模式用于定義實(shí)例屬性,而原型模式用于定義方法和共享屬性草雕。每個(gè)實(shí)例都會(huì)有自己的一份實(shí)例屬性,但同時(shí)又共享著方法固以,最大限度的節(jié)省了內(nèi)存墩虹。另外這種模式還支持傳遞初始參數(shù)。優(yōu)點(diǎn)甚多憨琳。這種模式在ECMAScript中是使用最廣泛诫钓、認(rèn)同度最高的一種創(chuàng)建自定義對(duì)象的方法。
6篙螟、動(dòng)態(tài)原型模式
function Blog(name, url) {
this.name = name;
this.url = url;
if (typeof this.alertInfo != 'function') {
// 這段代碼只執(zhí)行了一次
alert('exe time');
Blog.prototype.alertInfo = function() {
alert(thia.name + this.url);
}
}
}
var blog = new Blog('wuyuchang', 'http://tools.jb51.net');
var blog2 = new Blog('wyc', 'http:***.com');
動(dòng)態(tài)原型模式將所有信息封裝在了構(gòu)造函數(shù)中菌湃,而通過構(gòu)造函數(shù)中初始化原型(僅第一個(gè)對(duì)象實(shí)例化時(shí)初始化原型),這個(gè)可以通過判斷該方法是否有效而選擇是否需要初始化原型遍略【逅可以看到上面的例子中只彈出一次窗,'exe time',即當(dāng)blog初始化時(shí),這樣做blog2就不在需要初始化原型翎碑,對(duì)于使用這種模式創(chuàng)建對(duì)象呈野,可以算是perfect了。