在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/Java  HTML/ 重寫(xiě)原型對(duì)象之后,為什么最初的原型對(duì)象還會(huì)存在

重寫(xiě)原型對(duì)象之后,為什么最初的原型對(duì)象還會(huì)存在

        function Person(){}
        var friend=new Person();
        console.log(Person.prototype);            
        Person.prototype={                        
            constructor:Person,
            name:'ytf',
            age:'20',
            job:'student',
            sayName:function(){
                console.log(this.name);
            }
        }
        console.log(Person.prototype);
        console.log(friend.sayName());

clipboard.png

回答
編輯回答
別硬撐

因?yàn)槟阆葎?chuàng)建的對(duì)象, 舊的原型鏈已經(jīng)建立好了.

        function Person(){}
        // var friend=new Person(); 注釋掉
        console.log(Person.prototype);            
        Person.prototype={                        
            constructor:Person,
            name:'ytf',
            age:'20',
            job:'student',
            sayName:function(){
                console.log(this.name);
            }
        }
        var friend=new Person(); // 在這里創(chuàng)建
        console.log(Person.prototype);
        console.log(friend.sayName());
2018年6月2日 19:58
編輯回答
情殺

拆開(kāi)給你看,就明白了

var p_old = {};
function Person(){}
Person.prototype = p_old; // Person.prototype 指向的是一個(gè)對(duì)像

//var friend=new Person(); 差不多等同于如下三條語(yǔ)句
var friend = {};
friend.__porto__= Person.prototype // p_old;
Person.call(friend);

//然后你把 Person.prototype 指向了別的對(duì)像
Person.prototype = {
    //...
}

//但 friend.__porto__ 還是指向 p_old
console.log(friend.__porto__ == p_old) //true
2018年8月20日 21:57