JS中的_proto_

时间:2022-05-19 12:19:14
var grandfather = function(){
this.name = "LiuYashion"
this.age = ;
} var father = function(){};
father.prototype = new grandfather(); //继承父类
father.game = "LOL"; father.prototype.constructor = father; // 这声明相当于它自己独立出来 // 这里是给father添加额外属性,father.prototype.game是给father的原型grandfather添加额外属性
// 没有通过prototype继承的变量是无法继承的,即子类son中没有game属性 var son = function(){};
son.prototype = new father(); var temp1 = new grandfather();
console.log(temp1.__proto__); //object
console.log(grandfather.prototype) //object var temp2 = new father();
console.log(temp2.__proto__) //grandfather {name: "LiuYashion;", age: 23}
console.log(father.prototype) //grandfather {name: "LiuYashion", age: 23} // son var temp3 = new son();
console.log(son.prototype) // father {}一个原型链
console.log(temp3.__proto__) // father,因为上面黑粗体代码,让son类访问proto时只能指向father,不再是没写时的grandfather,相当于改变了此条原型链的基类