When I do the following:
function makeAddFunction(amount) {
function add(number) {
return number + amount;
}
return add;
}
var addTwo = makeAddFunction(2);
var 开发者_StackOverflowaddFive = 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? and does the order of variables entering and leaving the stack matter?
Each function call creates a new Function
(-Context). So to answer that quickly, yes they will have separate "stacks" in terms of 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). 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.
精彩评论