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

全面分析JavaScript 繼承

 更新時間:2019年05月30日 09:52:12   作者:文叔叔  
在JAVASCRIPT中,類的所有實(shí)例對象都是從同一個原型對象(父類)上繼承屬性,所以原型對象是類繼承機(jī)制的核心。或者是用戶自己定義的類,下面小編帶大家學(xué)習(xí)一下吧

ES6之前,JavaScript并沒有繼承這一現(xiàn)有的機(jī)制。

ES5的繼承方式

類式繼承

//聲明父類
function Father(){
this.fatherVal = 'father';
}
//為父類添加共有方法
Father.prototype.getFatherValue = function(){
return this.fatherVal;
}
//聲明子類 
function Child(){
this.childVal = 'child';
}
//繼承父類
Child.prototype = new Father();
//為子類添加共有方法
Child.prototype.getChildValue = function(){
return this.childVal;
}


子類的prototype被賦予父類的實(shí)例,新創(chuàng)建的對象復(fù)制了父類的構(gòu)造函數(shù)內(nèi)的屬性和方法并且將原型_proto_指向了父類的原型對象,這樣就擁有了父類的原型對象上的屬性和方法與父類構(gòu)造函數(shù)中復(fù)制的屬性和方法。

var instance = new Child();
console.log(instance.getFatherValue()); //father
console.log(instance.getChildValue()); //child
console.log(instance instanceof Child); //true
console.log(instance instanceof Father); //true
console.log(instance instanceof Object); //true
console.log(Child instanceof Father); //false
console.log(Child.prototype instanceof Father); //true


缺點(diǎn):

1.子類實(shí)例共用父類的公有引用屬性。

2.無法對父類構(gòu)造函數(shù)內(nèi)的屬性進(jìn)行傳參初始化。

function Father(){
this.companies =['bigo','yy','uc']
}
funtion Child(){}
Child.prototype = new Father();
var instanceA = new Child();
var instanceB = new Child();
console.log(instanceB.companies); //['bigo','yy','uc']
instanceA.companies.push('nemo');
console.log(instanceB.companies); //['bigo','yy','uc','nemo']

構(gòu)造函數(shù)繼承

//聲明父類
function Father(val){
this.companies =['bigo','yy','uc']
this.val = val;
}
//聲明父類原型方法
Father.prototype.getCom = function(){
console.log(this.companies);
}
//聲明子類
function Child(val){
//繼承
Father.call(this,val);
}
var instanceA = new Child('childA');
var instanceB = new Child('childB');
instanceA.companies.push('nemo');
console.log(instanceA.companies); //['bigo','yy','uc','nemo']
console.log(instanceA.val); //childA
console.log(instanceB.companies); //['bigo','yy','uc']
console.log(instanceB.val); //childB

對Child調(diào)用call,將子類中的變量在父類中執(zhí)行一遍,然后父類給this綁定,所以子類繼承了父類的公有屬性。

缺點(diǎn):

由于這種類型的繼承沒有設(shè)計(jì)原型prototype,所以父類的原型方法不會被子類繼承,而如果想被子類繼承就必須放在構(gòu)造函數(shù)中,這樣創(chuàng)建出來的每個實(shí)例都會單獨(dú)擁有一份而不能共用。

組合繼承

//聲明父類
function Father(val){
this.companies =['bigo','yy','uc']
this.val = val;
}
//聲明父類原型方法
Father.prototype.getValue = function(){
console.log(this.val);
}
//聲明子類
function Child(val,newVal){
//構(gòu)造函數(shù)式繼承
Father.call(this,val);
this.newVal = newVal;
}
//類式繼承
Child.prototype = new Father();
//聲明子類原型方法
Child.prototype.getNewValue = function(){
console.log(this.newVal);
}
var instanceA = new Child("fatherA","childA");
instanceA.companies.push('nemo');
console.log(instanceA.companies); //['bigo','yy','uc','nemo']
instanceA.getValue(); //fatherA
instanceA.getNewValue(); //childA
var instanceB = new Child("fatherB","childB");
console.log(instanceA.companies); //['bigo','yy','uc']
instanceB.getValue(); //fatherB
instanceB.getNewValue(); //childB


缺點(diǎn):

在使用構(gòu)造函數(shù)繼承使用執(zhí)行了一遍父類的構(gòu)造函數(shù),在實(shí)現(xiàn)子類原型的類式繼承再調(diào)用了一遍父類的構(gòu)造函數(shù),父類構(gòu)造函數(shù)被調(diào)用了兩次。

原型式繼承

function inheritObject(obj){
function F(){};
F.prototype = obj;
return new F();
}
var situation = {
companies:['bigo','yy','uc'];
area:'guangzhou';
}
var situationA = inheritObject(situation);
situationA.area = 'shenzhen';
situationA.companies.push('tencent');
var situationB = inheritObject(situation);
situationB.area = 'beijing';
situationB.companies.push('baidu');
console.log(situationA.area); //shenzhen
console.log(situationA.companies); //['bigo','yy','uc','tencent','baidu']
console.log(situationB.area); //beijing
console.log(situationB.companies); //['bigo','yy','uc','tencent','baidu']
console.log(situation.area); //guangzhou
console.log(situation.companies); //['bigo','yy','uc','tencent','baidu']


是類式繼承的一個封裝,其中的過渡對象就相當(dāng)于類式繼承的子類,然后返回新的實(shí)例化對象。

缺點(diǎn):

跟類式繼承一樣,父類的公有引用屬性被共有。

寄生式繼承

function inheritObject(obj){
function F(){};
F.prototype = obj;
return new F();
}
var situation = {
companies:['bigo','yy','uc'];
area:'guangzhou';
}
function createSituation(obj){
//通過原型繼承創(chuàng)建新對象
var newObj = new inheritObject(obj);
//定義新對象方法
newObj.getArea = function(){
console.log(newObj.area)
}
//返回對象
return obj;
}


只是在原型式繼承的基礎(chǔ)上添加了新屬性和方法,還是跟原型式繼承一樣的缺點(diǎn)。

寄生式組合繼承

function inheritObject(obj){
function F(){};
F.prototype = obj;
return new F();
}
//傳遞參數(shù) child,parent 子類父類
function inheritPrototype(child,parent){
//復(fù)制一份父類的原型副本保存在變量中;
var fatherProto = inheritObject(father.prototype);
//修正因?yàn)橹貙懽宇愒蛯?dǎo)致子類的constructor屬性被修改;
fatherProto.constructor = child;
//設(shè)置子類的原型
child.prototype = fatherProto;
}
//聲明父類
function Father(val){
this.companies =['bigo','yy','uc']
this.val = val;
}
//聲明父類原型方法
Father.prototype.getValue = function(){
console.log(this.val);
}
//聲明子類
function Child(val,newVal){
//構(gòu)造函數(shù)式繼承
Father.call(this,val);
this.newVal = newVal;
}
//類式繼承
Child.prototype = new Father();
inheritPrototype(Child,Father);
//聲明子類原型方法
Child.prototype.getNewValue = function(){
console.log(this.newVal);
}


1.在構(gòu)造函數(shù)繼承中我們已經(jīng)調(diào)用了父類的構(gòu)造函數(shù),還差一個原型的副本

2.通過原型繼承得到副本,但是這時候fatherProto的constructor需要指向子類。

3.最后將副本fatherProto賦給子類的原型prototype。

總的來說,就是既要構(gòu)造函數(shù),又要原型繼承,但是又避免了組合繼承的兩次調(diào)用父類構(gòu)造函數(shù)的問題,最大的改變式對子類原型賦予的式父類原型的一個引用。

var instanceA = new Child("fatherA","childA");
instanceA.companies.push('nemo');
console.log(instanceA.companies); //['bigo','yy','uc','nemo']
instanceA.getValue(); //fatherA
instanceA.getNewValue(); //childA
var instanceB = new Child("fatherB","childB");
console.log(instanceA.companies); //['bigo','yy','uc']
instanceB.getValue(); //fatherB
instanceB.getNewValue(); //childB

注意點(diǎn):

此時子類如果需要添加原型方法,必須通過prototype點(diǎn)語法一個個添加,否則會覆蓋掉繼承父類的原型對象。
ES6 新增了Class語法,Class 可以通過extends關(guān)鍵字實(shí)現(xiàn)繼承,這比 ES5 的通過修改原型鏈實(shí)現(xiàn)繼承,要清晰和方便很多。

Class 繼承

class Parent {
constructor(value) {
this.val = value
}
getValue() {
console.log(this.val)
}
}
class Child extends Parent {
constructor(value) {
super(value)
}
}
let child = new Child(1)
child.getValue() // 1
child instanceof Parent // true

class 實(shí)現(xiàn)繼承的核心在于使用 extends 表明繼承自哪個父類,并且在子類構(gòu)造函數(shù)中必須調(diào)用 super,因?yàn)檫@段代碼可以看成 Parent.call(this, value)。

如果子類沒有定義constructor方法,這個方法會被默認(rèn)添加。

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

相關(guān)文章

  • JavaScript 基礎(chǔ)問答二

    JavaScript 基礎(chǔ)問答二

    JavaScript的局限性分析
    2008-12-12
  • javascript基礎(chǔ)語法——全面理解變量和標(biāo)識符

    javascript基礎(chǔ)語法——全面理解變量和標(biāo)識符

    下面小編就為大家?guī)硪黄猨avascript基礎(chǔ)語法——全面理解變量和標(biāo)識符。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考,一起跟隨小編過來看看吧
    2016-06-06
  • JavaScript中的Math.SQRT1_2屬性使用簡介

    JavaScript中的Math.SQRT1_2屬性使用簡介

    這篇文章主要介紹了JavaScript中的Math.SQRT1_2屬性的使用,是JS入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-06-06
  • 簡單介紹JavaScript中字符串創(chuàng)建的基本方法

    簡單介紹JavaScript中字符串創(chuàng)建的基本方法

    這篇文章主要介紹了簡單介紹JavaScript中字符串創(chuàng)建的基本方法,是JS入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-07-07
  • 微信小程序中顯示html格式內(nèi)容的方法

    微信小程序中顯示html格式內(nèi)容的方法

    最近在工作中遇到一個需求,需要在不能顯示html格式的微信小程序中顯示html格式的內(nèi)容,通過查找相關(guān)的資料發(fā)現(xiàn)可以利用wxParse來實(shí)現(xiàn),下面這篇文章就主要給大家介紹了微信小程序?qū)崿F(xiàn)顯示html格式內(nèi)容的方法,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-04-04
  • 有關(guān)于JS構(gòu)造函數(shù)的重載和工廠方法

    有關(guān)于JS構(gòu)造函數(shù)的重載和工廠方法

    有關(guān)于JS構(gòu)造函數(shù)的重載和工廠方法,對此有需要的朋友,參考一下。
    2013-04-04
  • js的匿名函數(shù)使用介紹

    js的匿名函數(shù)使用介紹

    匿名函數(shù)的作用是創(chuàng)建一塊封閉區(qū)域,外面不能夠訪問里面的變量和方法,下面為大家介紹下什么是匿名函數(shù)及其如何使用
    2013-12-12
  • 深入探討JavaScript的最基本部分之執(zhí)行上下文

    深入探討JavaScript的最基本部分之執(zhí)行上下文

    今天小編就為大家分享一篇關(guān)于深入探討JavaScript的最基本部分之執(zhí)行上下文,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • Javascript基礎(chǔ)知識詳解

    Javascript基礎(chǔ)知識詳解

    本篇適合javascript新手或者學(xué)了前端一段時間,對js概念不清晰的同學(xué)。本文將講述幾點(diǎn)對于初學(xué)者遇到的javascript的坑,相信對javascript基礎(chǔ)薄弱的同學(xué),可以加深對javascript的理解
    2021-10-10
  • javaScript事件機(jī)制兼容【詳細(xì)整理】

    javaScript事件機(jī)制兼容【詳細(xì)整理】

    下面小編就為大家?guī)硪黄猨avaScript事件機(jī)制兼容【詳細(xì)整理】。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-07-07

最新評論

新邵县| 镇康县| 琼结县| 合肥市| 贵阳市| 鞍山市| 顺平县| 万州区| 安泽县| 永善县| 罗江县| 广宗县| 榕江县| 张家界市| 正镶白旗| 丰原市| 新宁县| 丰顺县| 双辽市| 奉贤区| 阳谷县| 伽师县| 谢通门县| 尤溪县| 衡南县| 旺苍县| 泽库县| 五台县| 磴口县| 定兴县| 临沧市| 桃源县| 吉林市| 临城县| 和政县| 油尖旺区| 弋阳县| 襄汾县| 昆明市| 安庆市| 呼图壁县|