欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript中六種面試常考繼承方式總結(jié)

 更新時間:2023年02月13日 09:14:59   作者:mick  
js的幾種繼承方式在我們面試的時候經(jīng)常會被問到,所以深入理解js幾種繼承方式以及它們的優(yōu)缺點(diǎn)是非常有必要的。本文為大家整理了JavaScript中面試??嫉牧N繼承方式,需要的可以參考一下

js的幾種繼承方式在我們面試的時候經(jīng)常會被問到,所以深入理解js幾種繼承方式以及它們的優(yōu)缺點(diǎn)是非常有必要的。

原型鏈繼承

之前我們介紹過原型和實例的關(guān)系:每一個構(gòu)造函數(shù)都有一個原型prototype,原型對象中的constructor又指回構(gòu)造函數(shù),實例中有一個內(nèi)部指針__proto__指向構(gòu)造函數(shù)的prototype。不清楚的可以看這篇 下面看個代碼

function Parent() {
  this.name = "mick";
}

Parent.prototype.getName = function () {
  console.log(this.name);
};

function Child() {}

Child.prototype = new Parent();

var child1 = new Child();

console.log(child1.getName());

我們將構(gòu)造函數(shù)Parent的實例賦值給了構(gòu)造函數(shù)Child的原型,實現(xiàn)了Child能夠繼承Parent的屬性和方法。

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

1.父類的方法可以被復(fù)用

缺點(diǎn)

1.父類的所有屬性都會被子類共享,只要修改了一個子類的引用類型的屬性,其他的子類也會受影響

function Parent() {
  this.names = ["mick", "randy"];
}

function Child() {}

Child.prototype = new Parent();

var child1 = new Child();

child1.names.push("qr");

console.log(child1.names); // ["mick", "randy", "qr"]

var child2 = new Child();

console.log(child2.names); //  ["mick", "randy", "qr"]

2.子類實例不能給父類構(gòu)造函數(shù)傳參

盜用構(gòu)造函數(shù)

盜用構(gòu)造函數(shù)的思路其實就是在子類構(gòu)造函數(shù)中通過call或者apply方法調(diào)用父類構(gòu)造函數(shù)

function Parent() {
  this.names = ["mick", "randy"];
}

function Child() {
  Parent.call(this);
}

var child1 = new Child();

child1.names.push("qr");

console.log(child1.names); // ["mick", "randy", "qr"]

var child2 = new Child();

console.log(child2.names); //  ["mick", "randy"]

這里我們通過在構(gòu)造函數(shù)Child中通過call調(diào)用Parent,此時this就是構(gòu)造函數(shù)Child,其實就是Child的實例被創(chuàng)建的時候都會對Parent進(jìn)行初始化,相當(dāng)于每一個實例都擁有了names屬性。

我們也可以給父構(gòu)造函數(shù)傳參了

function Parent(name) {
  this.name = name;
}

function Child(name, age) {
  Parent.call(this, name);
  this.age = age;
}

var child1 = new Child("randy", 18);

console.log(child1.name); // randy

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

  • 可以在子類構(gòu)造函數(shù)向父類構(gòu)造函數(shù)傳參
  • 父類的實例的引用屬性不會被共享

缺點(diǎn)

子類不能訪問父類原型上的方法,所以所有方法和屬性都寫在構(gòu)造函數(shù)中,每次實例創(chuàng)建都會被初始化。

組合繼承

組合繼承就是綜合原型鏈繼承和盜用構(gòu)造函數(shù)繼承的優(yōu)點(diǎn),從何又對這兩種方法的缺點(diǎn)互補(bǔ)。使用原型鏈繼承可以訪問父類原型上的屬性和方法,通過構(gòu)造函數(shù)繼承可以訪父類實例的屬性和方法。

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(); // 第二次
Child.prototype.constructor = Child;

var child1 = new Child("mick", "18");

child1.colors.push("black");

console.log(child1.name); // mick
console.log(child1.age); // 18
console.log(child1.colors); // ["red", "blue", "green", "black"]

var child2 = new Child("randy", "20");

console.log(child2.name); // randy
console.log(child2.age); // 20
console.log(child2.colors); // ["red", "blue", "green"]

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

  • 父類的方法可以復(fù)用
  • 可以在子類構(gòu)造函數(shù)向父類傳參
  • 父類構(gòu)造函數(shù)中的引用屬性不會共享

缺點(diǎn)

父類構(gòu)造函數(shù)被調(diào)用了兩次(文章中的注釋已經(jīng)標(biāo)出)

原型式繼承

創(chuàng)建一個臨時的構(gòu)造函數(shù),將傳入的對象賦值給這個構(gòu)造函數(shù)的原型,然后返回這個臨時類型的一個實例。其實就是對傳入對應(yīng)進(jìn)行一次淺復(fù)制。

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

其實就是Object.create的模擬實現(xiàn),將傳入的對象作為創(chuàng)建的對象的原型 缺點(diǎn)

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

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

let person = {
  name: "mick",
  colors: ["red", "blue", "green"],
};

let anotherPerson = createObj(person);
anotherPerson.name = "randy";
anotherPerson.colors.push("black");
console.log(anotherPerson.colors); // ['red', 'blue', 'green', 'black']
let yetAnotherPerson = createObj(person);

yetAnotherPerson.colors.push("yellow");
console.log(yetAnotherPerson.name); // mick
console.log(yetAnotherPerson.colors); // ['red', 'blue', 'green', 'black', 'yellow']

修改了anotherPerson.name的值,yetAnotherPerson.name沒有發(fā)生變化,這是因為anotherPerson.nameanotherPerson添加了name的值,并不是修改了原型上的值。

寄生式繼承

寄生式繼承背后類似于寄生構(gòu)造函數(shù)和工廠模式:創(chuàng)建一個實現(xiàn)繼承的函數(shù),以某種方式增強(qiáng)對象,然后返回這個對象。我們繼續(xù)使用原型式繼承創(chuàng)建的方法

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

function createAnother(original) {
  let clone = createObj(original);
  clone.sayHi = function () {
    console.log("hi");
  };
  return clone;
}

缺點(diǎn)

跟盜用構(gòu)造函數(shù)一樣的,方法在每次創(chuàng)建對象都會重新創(chuàng)建一遍

寄生式組合繼承

我們首先回看下組合繼承有個缺點(diǎn)就是父類構(gòu)造函數(shù)會調(diào)用兩次,那如何優(yōu)化這個缺點(diǎn)呢?

寄生式組合繼承通過盜用構(gòu)造函數(shù)繼承屬性,但使用混合式原型鏈繼承方法?;舅悸肥遣煌ㄟ^調(diào)用父類構(gòu)造函數(shù)給子類原型賦值,而是取得父類原型的一個副本。也就是使用寄生式繼承來繼承父類原型,然后將返回的新對象賦值給子類原型

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("mick", "18");

console.log(child1);

封裝一下

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

function prototype(child, Parent) {
  var prototype = createObj(parent.prototype);
  prototype.constructor = child;
  child.prototype = 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;
}

prototype(Child, Parent)

var child1 = new Child("mick", "18");

console.log(child1);

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

以上就是JavaScript中六種面試常考繼承方式總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于JavaScript繼承方式的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • js實現(xiàn)圖片粘貼到網(wǎng)頁

    js實現(xiàn)圖片粘貼到網(wǎng)頁

    這篇文章主要為大家詳細(xì)介紹了js實現(xiàn)圖片粘貼到網(wǎng)頁,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • js 如何刪除對象里的某個屬性

    js 如何刪除對象里的某個屬性

    這篇文章主要介紹了js 如何刪除對象里的某個屬性,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • 得到form下的所有的input的js代碼

    得到form下的所有的input的js代碼

    得到form下的所有的input的方法有很多,在本文為大家介紹下使用form 的集合對象elements,從而得到value,需要的朋友可以參考下
    2013-11-11
  • 如何利用Javascript生成平滑曲線詳解

    如何利用Javascript生成平滑曲線詳解

    相信大家都遇到過,在各種圖表框架中經(jīng)常會有將一段折線平滑的需求,不僅能給用戶帶來一種柔和的感覺,還能美化界面,讓折線看起來沒那么生硬,這篇文章主要給大家介紹了關(guān)于如何利用Javascript生成平滑曲線的相關(guān)資料,需要的朋友可以參考下
    2021-07-07
  • javascript結(jié)合html5 canvas實現(xiàn)(可調(diào)畫筆顏色/粗細(xì)/橡皮)的涂鴉板

    javascript結(jié)合html5 canvas實現(xiàn)(可調(diào)畫筆顏色/粗細(xì)/橡皮)的涂鴉板

    js+html5 canvas實現(xiàn)的涂鴉畫板特效,可調(diào)畫筆顏色|粗細(xì)|橡皮,可以保存涂鴉效果為圖片編碼,測試了下還不錯,感興趣的朋友可以參考下
    2013-04-04
  • js實現(xiàn)圖片輪播切換效果

    js實現(xiàn)圖片輪播切換效果

    這篇文章主要為大家詳細(xì)介紹了js實現(xiàn)圖片輪播切換效果,圖片自動輪播切換、點(diǎn)擊上下鍵圖片切換上下圖片等,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • WebRTC媒體權(quán)限申請getUserMedia實例詳解

    WebRTC媒體權(quán)限申請getUserMedia實例詳解

    這篇文章主要為大家介紹了WebRTC媒體權(quán)限申請getUserMedia實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • JS+Canvas繪制時鐘效果

    JS+Canvas繪制時鐘效果

    這篇文章主要為大家詳細(xì)介紹了基于javascript下使用canvas繪制時鐘的具體實現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Javascript中click與blur事件的順序詳析

    Javascript中click與blur事件的順序詳析

    這篇文章主要給大家介紹了關(guān)于Javascript中click與blur事件順序的相關(guān)資料,文中介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Javascript中的click與blur事件具有一定的參考學(xué)習(xí)價值,需要的朋友可以下面來一起看看吧。
    2017-04-04
  • ES6 Promise對象概念與用法分析

    ES6 Promise對象概念與用法分析

    這篇文章主要介紹了ES6 Promise對象概念與用法,簡單分析了Promise對象的基本狀態(tài)與三種重要方法,并結(jié)合實例形式給出相關(guān)使用技巧,需要的朋友可以參考下
    2017-04-04

最新評論