简体   繁体   English

这个 function 的堆栈会是什么样子?

[英]How will the stack of this function will look like?

When I do the following:当我执行以下操作时:

function makeAddFunction(amount) {
  function add(number) {
    return number + amount;
  }
  return add;
}

var addTwo = makeAddFunction(2);
var addFive = makeAddFunction(5);
alert(addTwo(1) + addFive(1));

Will each instance of makeAddFunction have a separate stack or all of them will use the same stack? makeAddFunction的每个实例是否都有单独的堆栈,或者它们都将使用相同的堆栈? and does the order of variables entering and leaving the stack matter?变量进入和离开堆栈的顺序是否重要?

Each function call creates a new Function (-Context).每个 function 调用都会创建一个新的Function (-Context)。 So to answer that quickly, yes they will have separate "stacks" in terms of ECMAscripts Execution Contexts .所以为了快速回答这个问题,是的,他们将在 ECMAscripts Execution Contexts方面拥有单独的“堆栈”。

I'm not so sure what you mean with "the order of variables entering and leaving the stack".我不太确定“变量进入和离开堆栈的顺序”是什么意思。

ECMAscript is all about contexts (objects). ECMAscript 是关于上下文(对象)的。 There is a stack of Execution Contexts which get called in order.有一堆按顺序调用的执行上下文。 After one context finished, the parent context continues to run until it's also finished (and so forth).在一个上下文完成后,父上下文继续运行,直到它也完成(等等)。 That principle lasts as long as there are any contextes if not, the Global context gets the attention again.只要有任何上下文,该原则就会持续,如果没有,则Global context会再次引起注意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM