亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

js組合繼承中斧正constructor的指向有什么用。
高洛峰
高洛峰 2016-11-10 09:22:57
0
3
928

Friend.prototype.constructor = Friend  
原先Friend.prototype.constructor指向的是Person
但是感覺(jué)并沒(méi)有什么用

有沒(méi)有彈出的結(jié)果都一樣,感覺(jué)都很好完成了繼承

function Person(name,age){
            this.name = name;
            this.age = age;
            if(typeof this.sayName != 'function'){
                Person.prototype.sayName = function(){
                    alert(this.name);
                }
            }
        }
        
        var per1 = new Person('zhang',23);
        var per2 = new Person('wagn',23);

        
        function Friend(name,age,sex){
            Person.call(this,name,age);
            this.sex = sex;
        } 
            Friend.prototype = new Person();
            Friend.prototype.constructor = Friend; //不斧正時(shí),constructor指向Person
            Friend.prototype.saySex=function(){
                alert(this.sex);
            }
        
        var fri1 = new Friend('wang','11','nan');
        var fri2 = new Friend('li','55','nv');

        alert(Person.prototype.constructor);


高洛峰
高洛峰

擁有18年軟件開(kāi)發(fā)和IT教學(xué)經(jīng)驗(yàn)。曾任多家上市公司技術(shù)總監(jiān)、架構(gòu)師、項(xiàng)目經(jīng)理、高級(jí)軟件工程師等職務(wù)。 網(wǎng)絡(luò)人氣名人講師,...

reply all(3)
三叔

關(guān)于constructor的修正,我想關(guān)乎到其后續(xù)使用吧,同時(shí)我覺(jué)得這也保證了面向?qū)ο蟮暮侠硇园?,保持constructor指向自家構(gòu)造器的特征

學(xué)霸

絕大多數(shù)情況是不用修正的,但是如果在不修正的情況下你這么寫(xiě) new Friend.prototype.constructor()返回的并不是一個(gè)Friend對(duì)象而只是Person對(duì)象。

三叔

用ES6的class語(yǔ)法理解應(yīng)該會(huì)好一點(diǎn),手機(jī)回復(fù),可以先參考mdn,如果不能理解,稍后補(bǔ)全

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template