在调试 JavaScript 原型(prototype)链时,我发现当我们定义一个 Constructor 函数时,constructor-->prototype 链是永无止境的,它会不断扩展。我附上了截图和示例代码,也供引用。
function Person(first, last, age, gender, interests) {
this.name = {
'first': first,
'last' : last
};
this.age = age;
this.gender = gender;
}
const person1 = new Person("foo", "bar", 24, "Male")
我的问题是,JavaScript 是通过引用/沿着链向上访问它还是 JavaScript 实际上将它存储在内存中
最佳答案
链不是一直在扩展的,它包含一个循环引用。
如 MDN 中所述:
Every constructor function has a
prototype
property whose value is an object containing aconstructor
property. Thisconstructor
property points to the original constructor function.
下面的例子演示了这一点:
function X() {}
const x = new X();
// test for strict equality
console.log(x.constructor === x.constructor.prototype.constructor); // true
关于javascript - 在 javascript "contructor --> prototype"链是永无止境的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69739369/