1、new 一个实例后,实例本身的prototype是【复制】类型本身的prototype.
Car.prototype.ppp = 'abc'; function Car() {}; let carObj = new Car(); // carObj.prototype.xxx = 'xxx' 错误,不被允许 Car.prototype = { ppp: '666' } Car.prototype = { ppp: '123' } Car.prototype.ppp = '888'; console.log(carObj.ppp); //输出 abc
2、new 实例后,再在【类型】本身的prototype添加的属性,是可以生效的。
3、
Car.prototype.ppp = 'abc'; Car.prototype.aaa = 'aaa'; function Car() {}; let carObj = new Car(); Car.prototype = { //这种写法会【打断】原型链, 不影响之前的类实例,只影响之后的类实例。 ppp: '123', bbb: 'bbb' } Car.prototype.xxx = 'xxx'; let carObj2 = new Car(); console.log(carObj2.ppp, carObj2.bbb, carObj.xxx); //输出 123 bbb undefined
function Car() {};
let carObj = new Car();
// carObj.prototype.xxx = 'xxx' 错误,不被允许
Car.prototype = { ppp: '666' }
Car.prototype = { ppp: '123' }
Car.prototype.ppp = '888';
console.log(carObj.ppp); //输出 abc