最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

深入理解JavaScript繼承的多種方式和優(yōu)缺點(diǎn)

 更新時(shí)間:2017年05月12日 14:24:46   作者:冴羽  
這篇文章主要介紹了深入理解JavaScript繼承的多種方式和優(yōu)缺點(diǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

寫在前面

本文講解JavaScript各種繼承方式和優(yōu)缺點(diǎn)。

注意:

跟《JavaScript深入之創(chuàng)建對象》一樣,更像是筆記。

哎,再讓我感嘆一句:《JavaScript高級程序設(shè)計(jì)》寫得真是太好了!

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.引用類型的屬性被所有實(shí)例共享,舉個例子:

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.在創(chuàng)建 Child 的實(shí)例時(shí),不能向Parent傳參

2.借用構(gòu)造函數(shù)(經(jīng)典繼承)

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"]

優(yōu)點(diǎn):

1.避免了引用類型的屬性被所有實(shí)例共享

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

缺點(diǎn):

方法都在構(gòu)造函數(shù)中定義,每次創(chuàng)建實(shí)例都會創(chuàng)建一遍方法。

3.組合繼承

原型鏈繼承和經(jīng)典繼承雙劍合璧。

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"]

優(yōu)點(diǎn):融合原型鏈繼承和構(gòu)造函數(shù)的優(yōu)點(diǎn),是 JavaScript 中最常用的繼承模式。

4.原型式繼承

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

就是 ES5 Object.create 的模擬實(shí)現(xiàn),將傳入的對象作為創(chuàng)建的對象的原型。

缺點(diǎn):

包含引用類型的屬性值始終都會共享相應(yīng)的值,這點(diǎn)跟原型鏈繼承一樣。

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的值并未發(fā)生改變,并不是因?yàn)?code>person1和person2有獨(dú)立的 name 值,而是因?yàn)?code>person1.name = 'person1',給person1添加了 name 值,并非修改了原型上的 name 值。

5. 寄生式繼承

創(chuàng)建一個僅用于封裝繼承過程的函數(shù),該函數(shù)在內(nèi)部以某種形式來做增強(qiáng)對象,最后返回對象。

function createObj (o) {
  var clone = object.create(o);
  clone.sayName = function () {
    console.log('hi');
  }
  return clone;
}

缺點(diǎn):跟借用構(gòu)造函數(shù)模式一樣,每次創(chuàng)建對象都會創(chuàng)建一遍方法。

6. 寄生組合式繼承

為了方便大家閱讀,在這里重復(fù)一下組合繼承的代碼:

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)

組合繼承最大的缺點(diǎn)是會調(diào)用兩次父構(gòu)造函數(shù)。

一次是設(shè)置子類型實(shí)例的原型的時(shí)候:

Child.prototype = new Parent();

一次在創(chuàng)建子類型實(shí)例的時(shí)候:

var child1 = new Child('kevin', '18');

回想下 new 的模擬實(shí)現(xiàn),其實(shí)在這句中,我們會執(zhí)行:

Parent.call(this, name);

在這里,我們又會調(diào)用了一次 Parent 構(gòu)造函數(shù)。

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

那么我們該如何精益求精,避免這一次重復(fù)調(diào)用呢?

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

看看如何實(shí)現(xiàn):

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

// 關(guān)鍵的三步
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;
}

// 當(dāng)我們使用的時(shí)候:
prototype(Child, Parent);

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

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • js 調(diào)用百度分享功能

    js 調(diào)用百度分享功能

    本文主要介紹了js調(diào)用百度分享功能的方法,具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-02-02
  • JS檢測數(shù)組類型的方法小結(jié)

    JS檢測數(shù)組類型的方法小結(jié)

    這篇文章主要介紹了js檢測數(shù)組類型的方法小結(jié),有instanceof方法Array.isArray() 方法和Object.prototype.toString.call()方法,都是比較常用的,需要的朋友可以參考下
    2017-03-03
  • 使用js獲取地址欄參數(shù)的方法推薦(超級簡單)

    使用js獲取地址欄參數(shù)的方法推薦(超級簡單)

    下面小編就為大家?guī)硪黄褂胘s獲取地址欄參數(shù)的方法推薦(超級簡單)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • js點(diǎn)擊任意區(qū)域彈出層消失實(shí)現(xiàn)代碼

    js點(diǎn)擊任意區(qū)域彈出層消失實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了js點(diǎn)擊任意區(qū)域彈出層消失實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • 深入淺析js原型鏈和vue構(gòu)造函數(shù)

    深入淺析js原型鏈和vue構(gòu)造函數(shù)

    這篇文章主要介紹了js原型鏈和vue構(gòu)造函數(shù)的相關(guān)知識,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2018-10-10
  • 微信小程序picker組件簡單用法示例【附demo源碼下載】

    微信小程序picker組件簡單用法示例【附demo源碼下載】

    這篇文章主要介紹了微信小程序picker組件簡單用法,結(jié)合實(shí)例形式詳細(xì)分析了picker組件的功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-12-12
  • postMessage消息通信Promise化的方法實(shí)現(xiàn)

    postMessage消息通信Promise化的方法實(shí)現(xiàn)

    postMessage Api 想必大家都不陌生,WebWorker 通信會用到,iframe 窗口之間通信也會用到,那么我們能不能將 postMessage 進(jìn)行一次轉(zhuǎn)化,把他變成類似 Promise 的使用方式,所以本文給大家介紹了postMessage消息通信Promise化的方法實(shí)現(xiàn),需要的朋友可以參考下
    2024-03-03
  • layui use 定義js外部引用函數(shù)的方法

    layui use 定義js外部引用函數(shù)的方法

    今天小編就為大家分享一篇layui use 定義js外部引用函數(shù)的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-09-09
  • 關(guān)于JS與jQuery中的文檔加載問題

    關(guān)于JS與jQuery中的文檔加載問題

    本文通過實(shí)例代碼給大家講解了js和jquery中的文檔加載問題,感興趣的的朋友一起看看吧
    2017-08-08
  • JavaScript實(shí)現(xiàn)打磚塊游戲

    JavaScript實(shí)現(xiàn)打磚塊游戲

    這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)打磚塊游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02

最新評論

靖西县| 平定县| 谷城县| 永定县| 西安市| 安福县| 射洪县| 福海县| 多伦县| 长阳| 交口县| 香港 | 云梦县| 宁远县| 利津县| 呼玛县| 盐池县| 华池县| 镇宁| 礼泉县| 钦州市| 建宁县| 黔南| 滕州市| 新竹市| 拉萨市| 庆城县| 宣汉县| 溆浦县| 库车县| 东山县| 岑溪市| 视频| 绿春县| 吉安市| 黑龙江省| 凤城市| 阳春市| 新民市| 梁山县| 青河县|