[英]In javascript runtime, what is the difference between a “frame” of the stack and a “message” of the queue?
In this article , I'm looking at this piece of code: 在本文中 ,我正在看这段代码:
function foo(b) {
var a = 10;
return a + b + 11;
}
function bar(x) {
var y = 3;
return foo(x * y);
}
console.log(bar(7)); //returns 42
And in the next paragraph, they say that each function bar, foo, are frames that are added to the stack. 在下一段中,他们说每个功能栏foo是添加到堆栈中的框架。
But I don't understand where the queue comes to the picture; 但是我不知道队列在哪里。 In that piece of code, where are the "messages" is the console.log() a message?
在那段代码中,console.log()的“消息”在哪里? How can we differ a message from a stack frame?
我们如何区分消息和堆栈框架?
You could add some outputs to the function which function is actually running an on stack while running. 您可以向函数添加一些输出,该函数在运行时实际上正在堆栈上运行。
Every function call creates a new stack entry and this entry pops after/with returning the function. 每个函数调用都会创建一个新的堆栈条目,并且在返回函数之后/之后该条目会弹出。
For better visalizing and getting a console.log
, the return value is chached in temp
and returned after showing the message. 为了更好地证明和获取
console.log
,返回值在temp
被替换,并在显示消息后返回。
function foo(b) { console.log('put foo on stack'); var a = 10, temp temp = a + b + 11; console.log('pop foo from stack'); return temp; } function bar(x) { console.log('put bar on stack'); var y = 3, temp; temp = foo(x * y); console.log('pop bar from stack'); return temp; } console.log(bar(7)); //returns 42
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.