javascript - 在 javascript "contructor --> prototyp

在调试 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 a constructor property. This constructor 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/

相关文章:

julia - 如何在 Julia 中生成随机日期?

java - 从 Double.toLongBits 创建的 long 中获取 double

javascript - React 中的 for 循环

c++ - 在 C++ 中迭代枚举类的常用方法是什么?

r - 如何从 R 中的多列创建合并值的新数据框

unity3d - Unity Vertical Layout Group 高度不计算子项

kotlin - 在 Android Compose 的 View 模型中使用 State

go - 在 Go 中将任意函数作为参数传递

postgresql - 在 PostgreSQL 中存储 UUID 的最佳数据类型是什么?

sorting - 如何按索引对 Vec 进行排序?