The following code has a new callstack when the debugger fires in d (jsfiddle here)
function c() {
setTimeout( d, 1000 );
}
function d() {
debugger;
}
c();
If we modify the code to use setTimeout( d(), 1000 );
which has brackets (parenthesis:)
function c() {
setTimeout( d(), 1000 );
}
function d() {
debugger;
}
c();
then the callstack has both c() and d() (jsfiddle here). Why?
Because in the first example you are passing a function pointer as the thing to execute in 1 second. In the second example you have already executed d, and you are passing the results of d() to setTimeout to call in 1 second.
You are not passing
setTimeout
the functiond
in the second example; you are instead passingd()
, which is the result of callingd
.The result of calling
d
isundefined
since it returns nothing, which converts to the string"undefined"
, which is theneval
ed, doing... precisely nothing.With regard to callstacks, since you are calling
d
inside ofc
, that is why you seec
in the callstack. To clarify, your second example is the same asSetTimeout takes a function argument. If you pass a string, it acts like eval. If you invoke the function, like you did, it fires immediately then setTimeout fires with the results in a new call stack.