国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

深入理解JavaScript繼承的多種方式和優缺點

2019-11-19 16:35:42
字體:
來源:轉載
供稿:網友

寫在前面

本文講解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

問題:

1.引用類型的屬性被所有實例共享,舉個例子:

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); // kevinvar 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); // kevinconsole.log(child1.age); // 18console.log(child1.colors); // ["red", "blue", "green", "black"]var child2 = new Child('daisy', '20');console.log(child2.name); // daisyconsole.log(child2.age); // 20console.log(child2.colors); // ["red", "blue", "green"]

優點:融合原型鏈繼承和構造函數的優點,是 JavaScript 中最常用的繼承模式。

4.原型式繼承

function createObj(o) {  function F(){}  F.prototype = o;  return new F();}

就是 ES5 Object.create 的模擬實現,將傳入的對象作為創建的對象的原型。

缺點:

包含引用類型的屬性值始終都會共享相應的值,這點跟原型鏈繼承一樣。

var person = {  name: 'kevin',  friends: ['daisy', 'kelly']}var person1 = createObj(person);var person2 = createObj(person);person1.name = 'person1';console.log(person2.name); // kevinperson1.firends.push('taylor');console.log(person2.friends); // ["daisy", "kelly", "taylor"]

注意:修改person1.name的值,person2.name的值并未發生改變,并不是因為person1person2有獨立的 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');

回想下 new 的模擬實現,其實在這句中,我們會執行:

Parent.call(this, name);

在這里,我們又會調用了一次 Parent 構造函數。

所以,在這個例子中,如果我們打印 child1 對象,我們會發現 Child.prototype 和 child1 都有一個屬性為colors,屬性值為['red', 'blue', 'green']。

那么我們該如何精益求精,避免這一次重復調用呢?

如果我們不使用 Child.prototype = new Parent() ,而是間接的讓 Child.prototype 訪問到 Parent.prototype 呢?

看看如何實現:

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);

引用《JavaScript高級程序設計》中對寄生組合式繼承的夸贊就是:

這種方式的高效率體現它只調用了一次Parent構造函數,并且因此避免了在 Parent.prototype 上面創建不必要的、多余的屬性。與此同時,原型鏈還能保持不變;因此,還能夠正常使用 instanceof 和 isPrototypeOf。開發人員普遍認為寄生組合式繼承是引用類型最理想的繼承范式。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乳源| 沙湾县| 济南市| 黄大仙区| 阿城市| 玛纳斯县| 凤山县| 资兴市| 泾阳县| 景德镇市| 金山区| 太康县| 永城市| 阿瓦提县| 陆河县| 香港 | 潼南县| 桐柏县| 江西省| 壶关县| 礼泉县| 望奎县| 北流市| 肃宁县| 汉寿县| 博湖县| 当涂县| 涞源县| 土默特右旗| 辛集市| 巴彦淖尔市| 昔阳县| 军事| 平南县| 胶南市| 合作市| 霍州市| 临朐县| 永城市| 金溪县| 建始县|