-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscopeChain.js
More file actions
52 lines (43 loc) · 1014 Bytes
/
Copy pathscopeChain.js
File metadata and controls
52 lines (43 loc) · 1014 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
Scope Chain - Scope chain is the process in which the JS look for the variable.
It will first look into its local scope --> then Outer Scope --> then Global Scope
*/
function b() {
var myVar = 3;
console.log("I am inside B function");
}
function a() {
b();
var myVar = 2;
}
var myVar = 1;
console.log(myVar);
// Creating Closures
function a() {
function b() {
var myVar = 3;
console.log("I am inside B function");
}
b();
console.log(myVar);
}
var myVar = 1;
console.log(myVar);
// Given example on StackOverflow
var currentScope = 0; // global scope
function a () {
var currentScope = 1, one = 'scope1';
alert(currentScope);
function b () {
var currentScope = 2, two = 'scope2';
alert(currentScope);
function c () {
var currentScope = 3, three = 'scope3';
alert(currentScope);
alert(one + two + three); // climb up the scope chain to get one and two
}
c();
}
b();
}
a();