了解JavaScript中的堆栈和框架 [英] Understanding stack and frame in javascript

查看:116
本文介绍了了解JavaScript中的堆栈和框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



因此,如果我有递归js函数,请执行所有递归调用在堆栈上创建一个堆栈框架?然后一旦我们回来就会弹出来?这个过程是否涉及堆?



接下来是关闭的想法。有了这个,我很困惑第一个函数放在堆上的地方在哪里?



任何解释都将不胜感激。

解决方案

b

所以如果我有递归js函数,所有的递归调用都会在堆栈上创建一个堆栈框架?


是。每一次函数调用都会创建一个新的堆栈框架。


然后一旦我们返回就会弹出来吗?
blockquote>

是的。目前在通用引擎中没有执行tail-call-elimination,并且它还没有被规范要求。


是堆参与这个过程?


可能。或者可能不是。该规范根本不区分堆栈和堆,而且无论如何您都无法通过语言来控制它。


我很困惑随着事情的发展。第一个函数是否会被放在堆上?


可能。如上所述,这是发动机的选择,但你可以期待它做出有教育的决定。

函数是JavaScript中的第一类对象,我希望它们可以放在堆中(注意在那里的递归调用通常只有一个函数,它们不会被重复)。堆栈中的堆栈帧中的变量仅包含原始值和指向对象的指针。


I am brand new to js, and am having trouble understanding what happens in the background.

So if I have a recursive js function, do all the recursive calls create a stack frame on the stack? and then get popped off once we return? Is the heap involved in this process?

Next the idea of closures. With this I am very confused with where things go does the first function get put on the heap?

Any explanation would be greatly appreciated.

解决方案

So if I have a recursive js function, do all the recursive calls create a stack frame on the stack?

Yes. Every function call creates a new stack frame.

and then get popped off once we return?

Yes. There is currently no tail-call-elimination implemented in common engines, and it's not (yet) required by the spec.

Is the heap involved in this process?

Possibly. Or maybe not. The specification does not distinguish between stack and heap at all, and you cannot control it by means of the language anyway.

I am very confused with where things go. Does the first function get put on the heap?

Possibly. As said above, it's the engine's choice where things go; but you can expect that it's making educated decisions.

Functions are first-class objects in JavaScript, and I'd expect them to be put in the heap (notice that in a recursive call there typically is only one function anyway, they don't get duplicated). The variables in the stack frames on the stack would only contain primitive values and pointers to objects.

这篇关于了解JavaScript中的堆栈和框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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