var triangle = { a: 1, b: 2, c: 3 };
Object.setPrototypeOf(triangle, { l: function add() {} });
function ColoredTriangle() {
this.color = "red";
}
ColoredTriangle.prototype = triangle;
var obj = new ColoredTriangle();
for (var prop in obj) {
console.log(`obj.${prop} = ${obj[prop]}`);
}
输出:
obj.color = red
obj.a = 1
obj.b = 2
obj.c = 3
obj.l = function add() {}
我们自定义的原型属性被打印了,但是对象的原型链上还有很多属性,为什么这些属性没被打印,难道是故意这么设计的?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…