Javascript无限原型链 [英] Javascript infinite prototype chain

查看:71
本文介绍了Javascript无限原型链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下测试代码:

function Test() {

 }

 Test.prototype.MyMethod =  {
    a: function() {

    },
    b: function() {

    }
 }

运行它我只是在做:

var test = new Test();
console.debug(test);

在firebug控制台中,我展开了打印的对象并查看 __ proto __

In the firebug console, I expand the object that was printed and look inside of __proto__:

在那里找到一个看似无穷无尽的构造函数链 - > prototype:

In there find a seemingly unending chain of constructor -> prototype:

+MyMethod
-constructor
  -prototype
     +MyMethod
     -constructor
        -prototype
           +MyMethod
           -constructor

依此类推。我在这里做错了吗?为什么原型链似乎无休止?

and so on. Did I do something wrong here? Why does the prototype chain seem unending?

推荐答案

prototype s有一个名为构造函数的属性,它引用拥有原型的函数。

prototypes have a property called constructor that refers to the function that owns the prototype.

此循环是设计使然。

这篇关于Javascript无限原型链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆