Asked
Active
Viewed 43 times
-5
-
2Please post the code and not a picture and this would be better suited for code review not stack overflow – user7951676 Sep 16 '17 at 06:49
-
2@user7951676 No! Explanation of code is off-topic at Code Review. – Mast Sep 16 '17 at 07:14
-
2I'm voting to close this question as off-topic because it does not contain the code in question – Zeta Sep 16 '17 at 07:45
1 Answers
0
I believe it should be because of lexical scoping or static scoping along with function scoping.
lexical scoping says variable scope is dependent on when the function was created in your cases when function b was created num was scoped to global.
function b() {
console.log(num)//num is scoped to its parent function it was created in
}
function a() {
var num = 3;//b will not have access to this num since it was not created here
arguments[0]();
}
num = 1;
a(test)
But if it were dynamic scoping then a variables scope is based on order of execution in which case num would have been scope to the caller a's num.
if you try this snippet then it will correctly print 3.
function a(cb) {
function b() {
console.log(num)
}//b is created here so it has access to num
var num = 3;
b();
}
num = 1;
a();
You can read about static scoping and dynamic scoping in this question where static vs dynamic, function vs block scope is explained better.

Shyam Babu
- 1,069
- 7
- 14