DIV CSS 佈局教程網

深入理解 JavaScript 對象繼承的幾種實現方式
編輯:關於JavaScript     

先從一個最直白的問題開始:JavaScript 裡的對象繼承到底是怎麼回事?

假設有一個「動物」構造函數:

 function animal() { this.type = '動物'; }

還有一個「貓」構造函數:

 function cat(name,color) { this.name = name; this.color = color; }

常識告訴我們貓也是動物。那麼,怎樣讓 cat 拿到 animal 的屬性?下面按由淺入深的順序梳理幾種做法。

方法一:構造函數綁定

最省事的思路,用 call 或 apply 把父對象綁到當前 this 上:

 function cat(name,color) { animal.apply(this,arguments); this.name = name; this.color = color; } var cat1 = new cat("haha", 'red'); console.log(cat1.type); //動物 

缺點也很明顯:每 new 一次都要重新執行一遍父構造函數,而且父對象原型上的方法拿不到。實戰中用得不多。

方法二:拷貝繼承

把父對象原型上的所有屬性和方法遍歷一遍,逐一拷到子對象原型上:

 function extend(Child, Parent) { var p = Parent.prototype; var c = Child.prototype; for (var i in p) { c[i] = p[i]; } c.uber = p; //橋梁作用 } 

使用:

 extend(cat, animal); var cat1 = new cat("haha","red"); alert(cat1.type); // 動物 

方法三:原型繼承

這是更主流的路子。先說清楚 prototype 是什麼:每個函數都有一個 prototype 屬性,它指向一個對象;用 new 創建實例時,實例會從這個原型對象上繼承屬性和方法。

所以,只要把 cat 的 prototype 指向一個 animal 的實例,貓的實例自然就有了動物身上的東西。

3.1 繼承實例

 cat.prototype = new animal(); cat.prototype.constructor = cat; var cat1 = new cat("haha","red"); console.log(cat1.constructor == cat); //true console.log(cat1.type); // 動物 

第一行把 cat.prototype 指向 animal 的實例,type 屬性隨之帶了過來。

第二行容易被忽略,但很關鍵。每個 prototype 對象都自帶一個 constructor 屬性,指向它的構造函數。如果我們直接替換掉整個 prototype,這個 constructor 還會停留在原來的位置:

 cat.prototype = new animal(); console.log(cat.prototype.constructor == animal); //true

再往下:

 cat.prototype = new animal(); var cat1 = new cat("haha", 'red'); console.log(cat1.constructor == animal); //true 

cat1 明明是 new cat() 出來的,constructor 卻指向 animal,這顯然不對。所以替換 prototype 之後,必須手動把 constructor 糾正回來:

 o.prototype = {}; o.prototype.constructor = o;

3.2 直接繼承 prototype

既然不變的屬性本來就該掛在 prototype 上,那不如先把 animal 改寫一下:

 function animal() { } animal.prototype.type = '動物';

然後直接讓 cat.prototype 指向 animal.prototype:

 cat.prototype = animal.prototype; cat.prototype.constructor = cat; var cat1 = new cat("haha","red"); console.log(cat1.type); // 動物 

這種寫法看著更高效——不用創建 animal 實例,省內存。但它其實是錯的:

 cat.prototype = animal.prototype;

這行代碼讓兩個變量指向了同一個對象。之後對 cat.prototype 的任何修改都會同步影響 animal.prototype。驗證一下:

 console.log(animal.prototype.constructor == animal); //false

之所以是 false,正是因為 cat.prototype.constructor = cat 這一行把 animal 的 constructor 也一起改掉了。

方法四:用空對象作中介

把上面兩種思路結合起來,就能兼得效率與隔離:

 var F = function(){}; F.prototype = animal.prototype; cat.prototype = new F(); cat.prototype.constructor = cat; 

F 是個空函數,幾乎不佔內存;而 cat.prototype 是 F 的實例,與 animal.prototype 之間隔了一層,改動互不干擾:

 console.log(animal.prototype.constructor == animal);  // true

封裝成通用函數:

 function extend(Child, Parent) { var F = function(){}; F.prototype = Parent.prototype; Child.prototype = new F(); Child.prototype.constructor = Child; Child.uber = Parent.prototype; }

使用時一行搞定:

 extend(cat,animal); var cat1 = new cat("haha","red"); console.log(cat1.type); // 動物 

最後那行 Child.uber = Parent.prototype 是個小技巧:在子對象上開一條名為 uber 的通道直指父原型,等於給子實例保留了回溯父類能力的入口。

以上就是個人對 JavaScript 對象繼承的理解,希望對你梳理這塊知識有點幫助。

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