简体   繁体   中英

Does built-in functions add in a call stack?

I am new in js and reading eloquent javascript book but the call stack part is confusing me. This is the sample code:

debugger;
function greet(who) { 
debugger;
  console.log("Hello " + who);
}
debugger;
greet("Harry");
debugger;
console.log("hi");  
console.log("bye");

This is what I have observed, debugger and console.log is an anonymous call. function greet gets defined in the global scope, but when I hit the line 7, there's still an anonymous call on the stack and function greet gets added on the stack? but why is there anonymous? can someone please tell me more about the call stack and what is happening here?

All code that is top-level (not in a function) is automatically moved into an internal function by the JS engine. Your code would be converted to:

(function() {
    debugger;
    function greet(who) { 
        debugger;
        console.log("Hello " + who);
    }
    debugger;
    greet("Harry");
    debugger;
    console.log("hi");  
    console.log("bye");
})();

This is the anonymous function that is at the bottom of the call stack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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