<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        全面分析JavaScript 繼承

        來源:懂視網(wǎng) 責編:小采 時間:2020-11-27 21:55:48
        文檔

        全面分析JavaScript 繼承

        全面分析JavaScript 繼承:ES6之前,JavaScript并沒有繼承這一現(xiàn)有的機制。 ES5的繼承方式 類式繼承 //聲明父類 function Father(){ this.fatherVal = 'father'; } //為父類添加共有方法 Father.prototype.getFatherValue = function(){
        推薦度:
        導讀全面分析JavaScript 繼承:ES6之前,JavaScript并沒有繼承這一現(xiàn)有的機制。 ES5的繼承方式 類式繼承 //聲明父類 function Father(){ this.fatherVal = 'father'; } //為父類添加共有方法 Father.prototype.getFatherValue = function(){

        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)建的對象復制了父類的構(gòu)造函數(shù)內(nèi)的屬性和方法并且將原型_proto_指向了父類的原型對象,這樣就擁有了父類的原型對象上的屬性和方法與父類構(gòu)造函數(shù)中復制的屬性和方法。

        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
        
        
        

        缺點:

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

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

        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綁定,所以子類繼承了父類的公有屬性。

        缺點:

        由于這種類型的繼承沒有設(shè)計原型prototype,所以父類的原型方法不會被子類繼承,而如果想被子類繼承就必須放在構(gòu)造函數(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){
        //構(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
        
        
        

        缺點:

        在使用構(gòu)造函數(shù)繼承使用執(zhí)行了一遍父類的構(gòu)造函數(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']
        
        
        

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

        缺點:

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

        寄生式繼承

        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ǔ)上添加了新屬性和方法,還是跟原型式繼承一樣的缺點。

        寄生式組合繼承

        function inheritObject(obj){
        function F(){};
        F.prototype = obj;
        return new F();
        }
        //傳遞參數(shù) child,parent 子類父類
        function inheritPrototype(child,parent){
        //復制一份父類的原型副本保存在變量中;
        var fatherProto = inheritObject(father.prototype);
        //修正因為重寫子類原型導致子類的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
        
        

        注意點:

        此時子類如果需要添加原型方法,必須通過prototype點語法一個個添加,否則會覆蓋掉繼承父類的原型對象。
        ES6 新增了Class語法,Class 可以通過extends關(guān)鍵字實現(xiàn)繼承,這比 ES5 的通過修改原型鏈實現(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 實現(xiàn)繼承的核心在于使用 extends 表明繼承自哪個父類,并且在子類構(gòu)造函數(shù)中必須調(diào)用 super,因為這段代碼可以看成 Parent.call(this, value)。

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

        聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        全面分析JavaScript 繼承

        全面分析JavaScript 繼承:ES6之前,JavaScript并沒有繼承這一現(xiàn)有的機制。 ES5的繼承方式 類式繼承 //聲明父類 function Father(){ this.fatherVal = 'father'; } //為父類添加共有方法 Father.prototype.getFatherValue = function(){
        推薦度:
        標簽: js 全面 解析
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 免费看的成人yellow视频| 成年人网站免费视频| 亚洲妇女无套内射精| 国产亚洲欧美日韩亚洲中文色| 亚洲国产精品无码久久久秋霞1| 美女网站免费福利视频| 亚洲高清最新av网站| 亚洲va无码va在线va天堂| 中文字幕亚洲综合久久2| 亚洲欧美国产国产综合一区| 三年片在线观看免费| 亚洲欧洲免费无码| 亚洲黄片手机免费观看| 亚洲日韩中文字幕| 国产精品免费久久| 性生交片免费无码看人| 亚洲色精品三区二区一区| 免费A级毛片av无码| 亚洲第一黄色网址| 中文字幕成人免费高清在线| 最近免费中文字幕视频高清在线看| 精品国产人成亚洲区| 国产成人亚洲综合一区| 免费人成激情视频在线观看冫| 亚洲精品无码不卡在线播HE| 亚洲日韩av无码中文| 亚洲 另类 无码 在线| 亚洲精品亚洲人成在线播放| 日本一卡精品视频免费| 亚洲精品一二三区| 亚洲国产成人VA在线观看| 两个人看www免费视频| 国产综合精品久久亚洲| 深夜久久AAAAA级毛片免费看| 四虎永久在线观看免费网站网址| 亚洲成av人无码亚洲成av人| 国内精品99亚洲免费高清| 99久久免费国产香蕉麻豆| 黑人粗长大战亚洲女2021国产精品成人免费视频 | 无套内谢孕妇毛片免费看看| 在线看片v免费观看视频777|