When trying to access a property of an object, the property will not only be sought on the object but on the prototype of the object, the prototype of the prototype, and so on until either a property with a matching name is found or the end of the prototype chain is reached.
这是否意味着当将属性添加到对象时,它就会添加到其原型(prototype)中?
即。在下面的代码中
a = {};
a.foo = "hello";
实际上存储为
a.prototype.foo = "hello"
最佳答案
没有。如果将属性存储在对象上,则将其存储在对象上。
它说,当您尝试访问它(即读取它,而不是写入它)时,如果在对象上找不到它,它将查找它的原型(prototype)链。
const a = {};
const b = Object.create(a);
a.foo = "Foo";
console.log(b.foo);
关于javascript - 对象的属性是否存储在其原型(prototype)中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58289677/