//工廠模式:
function createPerson(name, age, job) {
var o = new Object();
o.name = name;
o.age = age;
o.job = job;
o.say = function () {
document.write("Hello,I'm " + this.name + "," + this.age + " years old,i'm a " + this.job+"。<br/>");
}
return o;
}
//工廠模式的缺點是沒有解決對象識別問題,即判斷對象的類型:
var person1 = createPerson("wede.zhao", 29, "SoftWare");
document.write(person1 instanceof createPerson); //false
document.write(" --- ");
document.write(person1.constructor == createPerson); //false
document.write("<br/>");
//經過constructor屬性和instanceof操做符沒法正確得到工廠模式建立的對象類型,她們返回的都是基類Object類型:
document.write(person1 instanceof Object); //true
document.write(" --- ");
document.write(person1.constructor == Object); //true
document.write("<br/>");
//構造函數模式(構造函數在不返回值的狀況下,默認會返回新對象實例。):
function Person(name, age, job) {
this.name = name;
this.age = age;
this.job = job;
this.say = function () {
document.write("Hello,I'm " + this.name + "," + this.age + " years old,i'm a " + this.job + "。<br/>");
}
}
//構造函數解決了對象類型檢測問題:
var person2 = new Person("kitty", 18, "Student");
document.write(person2 instanceof Person); //true
document.write(" --- ");
document.write(person2.constructor == Person); //true
document.write("<br/>");
//寄生構造函數模式
function ParasiticPerson(name, age, job) {
var o = new Object();
o.name = name;
o.age = age;
o.job = job;
o.say = function () {
document.write("Hello,I'm " + this.name + "," + this.age + " years old,i'm a " + this.job + "。<br/>");
}
return o;
}
//爲何要用這種模式呢?答案是:若是在其餘模式(工廠模式、構造函數模式、原型模式等)都不適合的狀況下采用寄生構造函數模式- _ -!!!《高三》P160
//我的以爲,採用這種模式的一個優點在於它能夠返回重寫後的對象實例(好比說定製某個方法),而不是返回構造函數默認的對象實例。P161
//與工廠模式比較,區別:1,寄生構造函數模式用new建立新對象實例;2,寄生構造函數模式的包裝函數叫作構造函數,而不叫工廠;P160
var person3 = new ParasiticPerson("Nicholas", 29, "SoftWare Engineer");
//可是,與工廠模式相同的是,寄生構造函數模式的實例也不能正確檢測其類型:P161
document.write(person3 instanceof ParasiticPerson); //false
document.write(" --- ");
document.write(person3.constructor == ParasiticPerson); //false
document.write("<br/>");
document.write(person3 instanceof Object); //true
document.write(" --- ");
document.write(person3.constructor == Object); //true
document.write("<br/>");
//穩妥構造函數模式
function DurablePerson(name, age, job) {
var o = new Object();
o.say = function () {
document.write("Hello,I'm " + name + "," + age + " years old,i'm a " + job + "。<br/>"); //注意:不使用this,而使用參數傳入的變量
}
return o;
}
//與寄生構造函數模式有些類型,但不一樣點有二:1,構造函數內部不使用this;2,建立對象實例不使用new
var person4 = DurablePerson("james", 29, "Ball Star");
//使用穩妥構造函數模式的優勢是安全性。P161
//這種模式建立的實例也不能正確檢測其類型:
document.write(person4 instanceof DurablePerson); //false
document.write(" --- ");
document.write(person4.constructor == DurablePerson); //false
document.write("<br/>");
document.write(person4 instanceof Object); //true
document.write(" --- ");
document.write(person4.constructor == Object); //true
document.write("<br/>");
//總結:工廠模式、寄生構造函數模式、穩妥構造函數模式建立的實例都不能檢測其類型,共同的緣由是它們都在構造函數內部返回了另外一個對象(末尾都有return語句),
//而再也不是構造函數默認的對象(構造函數在不返回值的狀況下,默認會返回新對象實例)。
//這時候返回的對象與構造函數或構造函數的原型之間沒有關係,也就是說,經過構造函數返回的對象與在構造函數外部建立的對象同樣,
//這就是不能正確檢測其實例對象類型的根本緣由。安全