這篇文章主要介紹了深入理解JavaScript繼承的多種方式和優缺點,具有一定的參考價值,有興趣的小伙伴們可以參考一下
寫在前面
本文講解JavaScript各種繼承方式和優缺點。
注意:
跟《JavaScript深入之建立物件》一樣,更像是筆記。
哎,再讓我感嘆一句:《JavaScript高級程式設計》寫得真是太好了!
1.原型鏈繼承
function Parent () { this.name = 'kevin'; } Parent.prototype.getName = function () { console.log(this.name); } function Child () { } Child.prototype = new Parent(); var child1 = new Child(); console.log(child1.getName()) // kevin
問題:
function Parent () { this.names = ['kevin', 'daisy']; } function Child () { } Child.prototype = new Parent(); var child1 = new Child(); child1.names.push('yayu'); console.log(child1.names); // ["kevin", "daisy", "yayu"] var child2 = new Child(); console.log(child2.names); // ["kevin", "daisy", "yayu"]
2.在建立Child 的實例時,不能向Parent傳參
##2.借用建構子(經典繼承)
function Parent () { this.names = ['kevin', 'daisy']; } function Child () { Parent.call(this); } var child1 = new Child(); child1.names.push('yayu'); console.log(child1.names); // ["kevin", "daisy", "yayu"] var child2 = new Child(); console.log(child2.names); // ["kevin", "daisy"]
優點:
1.避免了參考類型的屬性被所有實例共用2.可以在Child 中向Parent 傳參舉例:function Parent (name) { this.name = name; } function Child (name) { Parent.call(this, name); } var child1 = new Child('kevin'); console.log(child1.name); // kevin var child2 = new Child('daisy'); console.log(child2.name); // daisy
3.組合繼承
function Parent (name) { this.name = name; this.colors = ['red', 'blue', 'green']; } Parent.prototype.getName = function () { console.log(this.name) } function Child (name, age) { Parent.call(this, name); this.age = age; } Child.prototype = new Parent(); var child1 = new Child('kevin', '18'); child1.colors.push('black'); console.log(child1.name); // kevin console.log(child1.age); // 18 console.log(child1.colors); // ["red", "blue", "green", "black"] var child2 = new Child('daisy', '20'); console.log(child2.name); // daisy console.log(child2.age); // 20 console.log(child2.colors); // ["red", "blue", "green"]
4.原型式繼承
function createObj(o) { function F(){} F.prototype = o; return new F(); }
Object.create 的模擬實現,將傳入的物件作為創建的物件的原型。
缺點:包含引用類型的屬性值總是會共用對應的值,這點就跟原型鏈繼承一樣。var person = { name: 'kevin', friends: ['daisy', 'kelly'] } var person1 = createObj(person); var person2 = createObj(person); person1.name = 'person1'; console.log(person2.name); // kevin person1.firends.push('taylor'); console.log(person2.friends); // ["daisy", "kelly", "taylor"]
person1.name的值,
person2.name的值並未改變,並不是因為
person1和
person2有獨立的name 值,而是因為
person1.name = 'person1',給
person1增加了name 值,並非修改了原型上的name 值。
5. 寄生式繼承
function createObj (o) { var clone = object.create(o); clone.sayName = function () { console.log('hi'); } return clone; }
6. 寄生組合式繼承
function Parent (name) { this.name = name; this.colors = ['red', 'blue', 'green']; } Parent.prototype.getName = function () { console.log(this.name) } function Child (name, age) { Parent.call(this, name); this.age = age; } Child.prototype = new Parent(); var child1 = new Child('kevin', '18'); console.log(child1)
Child.prototype = new Parent();
var child1 = new Child('kevin', '18');
Parent.call(this, name);
function Parent (name) { this.name = name; this.colors = ['red', 'blue', 'green']; } Parent.prototype.getName = function () { console.log(this.name) } function Child (name, age) { Parent.call(this, name); this.age = age; } // 关键的三步 var F = function () {}; F.prototype = Parent.prototype; Child.prototype = new F(); var child1 = new Child('kevin', '18'); console.log(child1);
function object(o) { function F() {} F.prototype = o; return new F(); } function prototype(child, parent) { var prototype = object(parent.prototype); prototype.constructor = child; child.prototype = prototype; } // 当我们使用的时候: prototype(Child, Parent);
以上是js各種繼承方式與優缺點的介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!