功能嵌套在原型中 [英] Function nested itself in the prototype

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

问题描述

问题1 :

当我注意到以下内容时,我正在尝试使用JS中的对象和构造函数的原型:

I was experimenting with prototypes of the objects and constructors in JS, when I've noticed that:

function f(a) {
  this.a = a;
  this.x = 1
}

function f2(a) {
  this.a = a;
  this.x = 4
}

f2.__proto__ = f(9);

var of2 = new f2(4);

以某种方式使f2()嵌套在其 prototype->中.构造函数,所以我有无限嵌套(如下所示):

somehow makes f2() nesting itself in its prototype -> constructor, so I've got infinite nesting (as below):

请问有人可以解释这种行为吗?还是仅仅是由工具引起的错误?

Could anyone explain such behaviour, please? Or maybe it's just an error, that is caused by tool?

问题2 :因此,如果正常,那该如何在内存中工作?它是在引用上工作,还是只是将原型插入构造函数,而将构造函数插入原型等等?如果第二个假设是正确的,为什么我的计算机内存仍然很好并且没有燃烧?

Question2: So if it's normal, how does that work in the memory? Is it working on references, or just insert prototype to constructor, constructor to prototype an so on? If the second hypothesis is true, why my computers memory is still fine and not burning?

推荐答案

它与

look to the right ->             <- look to the left

所以实际上它是相当有限的;)

so actually its quite finite ;)

prototype ->                   <- constructor

(提示:此答案在300px智能手机上效果最佳..))

( hint: this answer works best on a 300px smartphone.. ;))

这篇关于功能嵌套在原型中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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