ADI*_*IKA 2 javascript prototype node.js typescript
在调试 JavaScript 原型链时,我发现当我们定义构造函数时,构造函数-->原型链是永无止境的,它不断扩展。我附上了屏幕截图和示例代码,也供参考。

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")
Run Code Online (Sandbox Code Playgroud)
我的问题是,JavaScript 是通过引用/沿着链向上访问它还是 JavaScript 实际上将其存储在内存中
该链不会不断扩展,它包含循环引用。
正如MDN上所解释的:
每个构造函数都有一个
prototype属性,其值是包含属性的对象constructor。该constructor属性指向原始构造函数。
以下示例演示了这一点:
function X() {}
const x = new X();
// test for strict equality
console.log(x.constructor === x.constructor.prototype.constructor); // trueRun Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
117 次 |
| 最近记录: |