My question is about the execution context in this code: when the function check() got called using the console.log will the input() function execution context be inside the check() function execution context or outside of it .
var x;
var y;
function check(){
y = input()
if(y<10){
return "true";
}
else{
return false;
}
}
function input(){
x = eval(prompt("please enter a number "));
return x;
}
console.log(check());
I tried just to make the variable deceleration outside of the function and assign them inside the function.
The result that i got when i added input as (9) i got true and that is correct.the question is about the context only.
It's not "inside". Execution contexts are one on top of the other on a stack.
You have the Global Execution Context at the bottom. When you call check()
, its execution context is pushed on top of the stack.
check()
-------
Global
When inside check
you call input()
, the new execution context will be again pushed on top:
input()
-------
check()
-------
Global
When input
returns a value, its context is popped out of the array, and execution of check
resumes. When check
returns, its context is popped out too.
Both functions have access to variables x
and y
through the scope chain.
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.