After rewriting the prototype object, why does the original prototype object still exist

        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

May.05,2021

because of the object you created first, the old prototype chain has been established.

var p_old = {};
function Person(){}
Person.prototype = p_old; // Person.prototype 

//var friend=new Person(); 
var friend = {};
friend.__porto__= Person.prototype // p_old;
Person.call(friend);

// Person.prototype 
Person.prototype = {
    //...
}

// friend.__porto__  p_old
console.log(friend.__porto__ == p_old) //true
Menu