DIV CSS 佈局教程網

JavaScript 物件建立模式總整理:從工廠模式到穩妥建構函式
編輯:關於JavaScript     

在 JavaScript 中建立物件看似簡單——字面值或 new 都可以——但一旦需要建立大量同類物件,各種模式的差異就會浮現:能不能辨識物件類型?方法會不會被重複配置?資料會不會在實例之間互相污染?本文系統性整理七種經典模式。

一、工廠模式

用函式封裝建立物件的細節,把具體過程抽象化。

 function createAnimal(name, age) { var o = new Object(); o.name = name; o.age = age; o.sayName = function() { alert(this.name); } return o; } var cat = createAnimal("cat", 12); var dog = createAnimal("dog", 3);

它解決了「重複撰寫相似物件」的問題,卻沒有解決物件識別——cat 與 dog 都只是一般 Object,無法判斷是哪種類型。另外每個實例都各自持有一份 sayName 函式,造成記憶體浪費。

二、建構函式模式

 function Animal(name, age) { this.name = name; this.age = age; this.sayName = function() { alert(this.name); } } var cat = new Animal("cat", 12); var dog = new Animal("dog", 3); 

用 new 呼叫時,引擎會建立新物件、把 this 綁定到它、執行函式體、再回傳該物件(除非函式自己回傳了物件)。此時可用 constructor 或 instanceof 辨識類型:

 cat.constructor == Animal // true cat instanceof Animal // true

問題是 sayName 仍寫在建構函式內,每 new 一次就產生一個新函式,cat.sayName === dog.sayName 為 false。要共用方法,得靠原型。

三、原型模式

每個函式都有一個 prototype 屬性,指向一個物件;掛在該物件上的屬性與方法,會被所有實例共享。

 function Animal() {} Animal.prototype.name = "animal"; Animal.prototype.age = 1; Animal.prototype.sayName = function() { alert(this.name); } var test1 = new Animal(); test1.sayName(); // "animal" var test2 = new Animal(); test2.sayName(); // "animal" alert(test1.sayName === test2.sayName); // true 

也可以一次替換整個原型物件,但要記得補回 constructor,否則它會指向 Object:

 function Animal() {} Animal.prototype = { constructor: Animal, name: "animal", age: 1, sayName: function() { alert(this.name); } };

原型的致命弱點在引用型別。所有實例共享同一個陣列或物件,一處修改處處可見:

 function Animal() {} Animal.prototype = { constructor: Animal, name: "animal", age: 1, hobbies: ["dance", "sing", "play"], sayName: function() { alert(this.name); } }; var cat = new Animal(); var dog = new Animal(); cat.hobbies.push("sleep"); alert(cat.hobbies); // "dance", "sing", "play", "sleep" alert(dog.hobbies); // "dance", "sing", "play", "sleep" alert(cat.hobbies === dog.hobbies); // true 

dog 的興趣莫名多了一項「sleep」,這正是共享原型造成的典型 bug。

四、組合使用建構函式與原型

最經典的折衷方案:實例屬性放建構函式,方法放原型。

 function Animal(name, age) { this.name = "animal"; this.age = 1; this.hobbies = ["dance", "sing", "play"]; } Animal.prototype = { constructor: Animal, sayName: function() { alert(this.name); } }; var cat = new Animal("cat", 2); var dog = new Animal("dog", 3); cat.hobbies.push("sleep"); alert(cat.hobbies); // "dance", "sing", "play", "sleep" alert(dog.hobbies); // "dance", "sing", "play" alert(cat.hobbies === dog.hobbies); // false alert(cat.sayName === dog.sayName); // true 

每個實例有自己的 hobbies(互不干擾),sayName 則共用一份。這也是 ES6 class 背後的實作方式。

五、動態原型模式

 function Animal(name, age) { this.name = name; this.age = age; if(typeof this.sayName != "function") { Animal.prototype.sayName = function() { alert(this.name); } } } var cat = new Animal("cat", 12); cat.sayName(); // "cat" 

只在第一次建立實例時才初始化原型方法,之後跳過。使用時有個限制:不能用物件字面值重新覆寫整個原型,因為那會切斷既有實例與新原型之間的連結,導致先前建立的物件失去方法。

六、寄生建構函式模式

基本思路是建立一個函式,僅負責封裝建立物件的程式碼,然後回傳新物件。除了用 new 呼叫之外,長得跟工廠模式一模一樣。它靠的是一個語言特性:建構函式若不回傳值,預設回傳新實例;若在尾端加上 return,就能改寫回傳內容。

 function Animal(name, age) { var o = new Object(); o.name = name; o.age = age; o.sayName = function() { alert(this.name); } return o; } var cat = new Animal("cat", 12); cat.sayName(); // "cat"

由於回傳的物件與建構函式及其原型毫無關聯,instanceof 無法用來判斷類型。除非有特殊需求(例如想為現有的、無法修改的建構函式包裝附加功能),否則不建議使用。

七、穩妥建構函式模式

與寄生模式類似,但有兩點不同:新物件的實例方法不引用 this;不使用 new 呼叫。

 function Animal(name, age) { var o = new Object(); o.name = name; o.age = age; var msg = "Hello, I'm "; o.sayName = function() { alert(msg + this.name); } return o; } var cat = new Animal("cat", 12); cat.sayName(); // "Hello, I'm cat" 

「穩妥」之處在於:即使函式被以非建構方式呼叫、環境中沒有 this、或 this 被挾持,物件依然能正常運作。適合用在沙箱化環境或需要高度安全隔離的場合,例如某些廣告腳本、瀏覽器擴充功能的隔離環境。

總結對照

實務上,若使用 ES6 以上語法,直接用 class 搭配 constructor 與方法定義即可,它語意上就是「原型屬性放建構函式、方法放原型」的組合模式,並額外提供繼承、super 呼叫與更清楚的錯誤提示。若必須停留在 ES5,組合模式是最穩定的選擇;需要資料隔離又不想用 new 時,可考慮穩妥建構函式。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved