javascript - 构造函数原型(prototype)循环引用 JavaScript

标签 javascript constructor prototype

我正在写一篇关于函数对象的构造函数属性的博客,并遇到了这一行:

The prototype property of a function Object has the constructor property set to the function itself

含义,如下函数对象

function Student(name,age) {
    this.name = name;
    this.age = age;
}

将有以下原型(prototype)

{构造函数:学生}

    function Student() {
    }

    console.log(Student.prototype);
    console.log(Student.prototype.constructor);
    console.log(Student.prototype.constructor.prototype);
    console.log(Student.prototype.constructor.prototype.constructor);
    console.log(Student.prototype.constructor.prototype.constructor.prototype);

这意味着原型(prototype)具有constructor属性,该属性设置为具有相同原型(prototype)对象的函数本身。这是否有某些原因,或者只是一种语言功能。我找不到任何在这里进行循环引用的理由。

任何帮助表示赞赏。谢谢。

最佳答案

prototype.constructor 属性设置为引用构造函数,以便该构造函数实例化的对象可以检查使用哪个构造函数来创建它们。

关于javascript - 构造函数原型(prototype)循环引用 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29116041/

相关文章:

javascript - 用于替换字符串中脏话的正则表达式

javascript - 这种寄生遗传模式好不好?

javascript - 为什么异步 XMLHttpRequests 优于同步请求?

c++ - 插入 >> 运算符重载 : exception handling when retrieving object's ctor parameters from cin

c++ - 试图理解 C++ 类之间的组合

javascript - Angular CLI - 如何在整个应用程序中共享原型(prototype)函数

javascript - 旋转多边形检测并使直线遵循边界

c++ - 放置类构造函数

Javascript函数和原型(prototype)——通过调用方法的基本路由问题

javascript - 什么是 JavaScript 中的原型(prototype)?