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

首頁 > 語言 > JavaScript > 正文

全面分析JavaScript 繼承

2024-05-06 15:38:42
字體:
來源:轉載
供稿:網(wǎng)友

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

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被賦予父類的實例,新創(chuàng)建的對象復制了父類的構造函數(shù)內的屬性和方法并且將原型_proto_指向了父類的原型對象,這樣就擁有了父類的原型對象上的屬性和方法與父類構造函數(shù)中復制的屬性和方法。

var instance = new Child();console.log(instance.getFatherValue()); //fatherconsole.log(instance.getChildValue()); //childconsole.log(instance instanceof Child); //trueconsole.log(instance instanceof Father); //trueconsole.log(instance instanceof Object); //trueconsole.log(Child instanceof Father); //falseconsole.log(Child.prototype instanceof Father); //true

缺點:

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

2.無法對父類構造函數(shù)內的屬性進行傳參初始化。

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

構造函數(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); //childAconsole.log(instanceB.companies); //['bigo','yy','uc']console.log(instanceB.val); //childB

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

缺點:

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

組合繼承

//聲明父類function Father(val){this.companies =['bigo','yy','uc']this.val = val;}//聲明父類原型方法Father.prototype.getValue = function(){console.log(this.val);}//聲明子類function Child(val,newVal){//構造函數(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(); //fatherAinstanceA.getNewValue(); //childAvar instanceB = new Child("fatherB","childB");console.log(instanceA.companies); //['bigo','yy','uc']instanceB.getValue(); //fatherBinstanceB.getNewValue(); //childB            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 扬中市| 金坛市| 蒙自县| 宁强县| 巍山| 武威市| 河南省| 仁布县| 伊春市| 济宁市| 中西区| 天柱县| 汤阴县| 淄博市| 肥西县| 克山县| 合水县| 许昌市| 台中市| 望奎县| 大方县| 军事| 鹤壁市| 千阳县| 渭源县| 贵溪市| 普兰县| 黄平县| 田阳县| 岗巴县| 邵阳市| 称多县| 闸北区| 晋宁县| 天柱县| 白沙| 阜阳市| 平远县| 甘泉县| 永川市| 陕西省|