-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscope.js
More file actions
69 lines (49 loc) · 1.96 KB
/
Copy pathscope.js
File metadata and controls
69 lines (49 loc) · 1.96 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// if the javascript engine does not find the variable in local scope,
// it tries to check for the variable in the outer scope. If the variable
// does not exist in the outer scope, it tries to find the variable in the global scope.
/* =========Global scope=========*/
var globalVariable = "Hello world";
function sendMessage(){
return globalVariable; // can access globalVariable since it's written in global space
}
function sendMessage2(){
return sendMessage(); // Can access sendMessage function since it's written in global space
}
console.log(sendMessage2()); // Returns “Hello world”
/* =========Function scope=========*/
function awesomeFunction(){
var a = 2;
var multiplyBy2 = function(){
console.log(a*2); // Can access variable "a" since a and multiplyBy2 both are written inside the same function
}
}
// Throws reference error since a is written in local scope and cannot be accessed outside
// console.log(a);
// Throws reference error since multiplyBy2 is written in local scope
// console.log(multiplyBy2());
/*========Block scope======== */
{
let x = 45;
console.log("Access inside block: x = " + x);
}
// Gives reference error since x cannot be accessed outside of the block
// console.log("Access outside block: x = " + x);
for(let i=0; i<2; i++){
console.log(i);
}
// Gives reference error since i cannot be accessed outside of the for loop block
// console.log(i);
/*Example: scope chaining */
var y = 24;
function favFunction(){
var x = 667;
var anotherFavFunction = function(){
console.log(x); // Does not find x inside anotherFavFunction, so looks for variable inside favFunction, outputs 667
}
var yetAnotherFavFunction = function(){
console.log(y); // Does not find y inside yetAnotherFavFunction, so looks for variable inside favFunction and does not find it, so looks for variable in global scope, finds it and outputs 24
}
anotherFavFunction();
yetAnotherFavFunction();
}
favFunction();