我有一个关于JavaScript对象原型链的问题。假设我创建了一个对象
var first = { a: 1};
var second = Object.create(first);现在我知道了,如果我在第二个对象上查找属性a,由于原型继承,我会得到值1。但是,假设我将second赋值给第一个对象的隐藏__ proto__属性,那么查找不应该陷入查找周期吗?
我的意思是:
first.__proto__ = second;
cosole.log(second.z); //Would it keep looking for both objects in a cycle?发布于 2015-03-28 00:54:33
它不会进入无限循环,因为它不需要查看第二个对象中的内容。相反,它显示存在第二个对象,如果您想进一步了解它,可以在单独的步骤中完成。
以下是我在伪代码中的一个快速示例,以使其更容易理解:
OBJ 1 = {a:1}
OBJ 2 = Prototype of OBJ 1
Prototype of OBJ 1 = OBJ 2
print OBJ 2
Shows:
// Once again for clarity:
// The reason it is not entering into an infinite loop is because
// it doesn't iterate through the prototype object and instead it SHOWS the prototype.
// If you later want to see what's inside of it you can do so with an extra step.
// But it gives you minimal available information and so you see this:
a: 1
proto: OBJ 1https://stackoverflow.com/questions/29351698
复制相似问题