SlideShare a Scribd company logo
1 of 55
JS compilation - execution
What we will see . . .
● Compilation
● Execution
● Variable shadowing
● Variable hoisting
● Closures
● Execution context stack
● Event loop
Compilation Phase
● Creates the variable object: Variable object is a special object in JS which contain all the variables, function arguments and
inner functions declarations information.
● Creates the scope chain
● Determines the value of this
Extracts all the declarations (variable, function). It prepares the memory so that it can execute the code
variableObject: {
argumentObject: { }
},
scopechain: [],
This
Execution Phase
JavaScript is a single threaded language, meaning only one task can be executed at a time. When the JavaScript interpreter initially
executes code, it first enters into a global execution context by default. Each invocation of a function from this point on will result in
the creation of a new execution context.
● Global execution context
● Functional execution context
executionContextObj = {
variableObject: {
argumentObject: { }
},
scopechain: [],
this
}
Compilationglobal
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
globalExecutionContextObj = {
variableObject: {
argumentObject: { }
},
scopechain: [],
this
}
>
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
globalExecutionContextObj = {
variableObject: {
argumentObject: { },
a: undefined
},
scopechain: [],
this
}
>
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
globalExecutionContextObj = {
variableObject: {
argumentObject: { },
a: undefined
},
scopechain: [],
this
}
> no declaration found on this line. The JS engine moves on the next
line
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
globalExecutionContextObj = {
variableObject: {
argumentObject: { },
a: undefined,
funcName: pointer to heap memory in the function definition
},
scopechain: [],
this
}
> Whenever the JS engines finds a function declaration, creates a
property and points to heap memory where the function definition is
stored
> Moves to line 23
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
globalExecutionContextObj = {
variableObject: {
argumentObject: { },
a: undefined,
funcName: pointer to heap memory in the function definition
},
scopechain: [],
this
}
> This is not a declaration. Code won’t do anything here
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
globalExecutionContextObj = {
variableObject: {
argumentObject: {
length: 0
},
a: undefined,
funcName: pointer to heap memory in the function definition
},
scopechain: [Global execution context],
this: value of this
}
> end of code. Hence...
> scopechain is set
> this is set
Executionglobal
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
globalExecutionContextObj = {
variableObject: {
argumentObject: {
length: 0
},
a: 2,
funcName: pointer to heap memory in the function definition
},
scopechain: [Global execution context],
this: value of this
}
>
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
globalExecutionContextObj = {
variableObject: {
argumentObject: {
length: 0
},
a: 2,
funcName: pointer to heap memory in the function definition,
b: 1
},
scopechain: [Global execution context],
this: value of this
}
> No property with name b found. JS engine will create it and
initialize it with value 1
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
globalExecutionContextObj = {
variableObject: {
argumentObject: {
length: 0
},
a: 2,
funcName: pointer to heap memory in the function definition,
b: 1
},
scopechain: [Global execution context],
this: value of this
}
> No property with name b found. JS engine will create it and
initialize it with value 1
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
globalExecutionContextObj = {
variableObject: {
argumentObject: {
length: 0
},
a: 2,
funcName: pointer to heap memory in the function definition,
b: 1
},
scopechain: [Global execution context],
this: value of this
}
> Since it’s a function declaration the engine won’t do anything, and
moves to line 23
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
globalExecutionContextObj = {
variableObject: {
argumentObject: {
length: 0
},
a: 2,
funcName: pointer to heap memory in the function definition,
b: 1
},
scopechain: [Global execution context],
this: value of this
}
>
CompilationfuncName
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
funcNameExecutionContextObj = {
variableObject: {
argumentObject: {}
},
scopechain: [],
this:
}
> No arguments specified. Nothing will added in the argumentObject
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
funcNameExecutionContextObj = {
variableObject: {
argumentObject: {},
foo: undefined
},
scopechain: [],
this:
}
>
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
funcNameExecutionContextObj = {
variableObject: {
argumentObject: {},
foo: undefined,
funcNameA: pointer to heap memory in the function definition
},
scopechain: [],
this:
}
>
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
funcNameExecutionContextObj = {
variableObject: {
argumentObject: {},
foo: undefined,
funcNameA: pointer to heap memory in the function definition
},
scopechain: [],
this:
}
> This is not a declaration. Code won’t do anything here
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
funcNameExecutionContextObj = {
variableObject: {
argumentObject: {},
foo: undefined,
funcNameA: pointer to heap memory in the function definition,
bar: undefined
},
scopechain: [],
this:
}
>
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
funcNameExecutionContextObj = {
variableObject: {
argumentObject: {},
foo: undefined,
funcNameA: pointer to heap memory in the function definition,
bar: undefined
},
scopechain: [],
this:
}
> This is not a declaration. Code won’t do anything here
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
funcNameExecutionContextObj = {
variableObject: {
argumentObject: {},
foo: undefined,
funcNameA: pointer to heap memory in the function definition,
bar: undefined
},
scopechain: [funcName execution context, Global execution context],
this: value of this
}
> end of code. Hence...
> scopechain is set
> this is set
>
> As there is no other code, JS engine will start the execution phase
ExecutionfuncName
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
funcNameExecutionContextObj = {
variableObject: {
argumentObject: {
length: 0
},
foo: “foo”,
funcNameA: pointer to heap memory in the function definition,
bar: undefined
},
scopechain: [funcName execution context, Global execution context],
this: value of this
}
>
funcNameExecutionContextObj = {
variableObject: {
argumentObject: {
length: 0
},
foo: “foo”,
funcNameA: pointer to heap memory in the function definition,
bar: undefined
},
scopechain: [funcName execution context, Global execution context],
this: value of this
}
> It’s a function declaration, JS engine won’t do anything and moves
to line 17
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
funcNameExecutionContextObj = {
variableObject: {
argumentObject: {
length: 0
},
foo: “foo”,
funcNameA: pointer to heap memory in the function definition,
bar: undefined
},
scopechain: [funcName execution context, Global execution context],
this: value of this
}
globalExecutionContextObj = {
variableObject: {
argumentObject: {
length: 0
},
a: 2,
funcName: pointer to heap memory in the function definition,
b: 1,
foo1: “foo value”
},
scopechain: [Global execution context],
this: value of this
}
> There is no foo1 variable in the execution context. JS engines asks
the next execution context of the scope chain if this variable
exists. It doesn’t, so it creates it and assign the value
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
funcNameExecutionContextObj = {
variableObject: {
argumentObject: {
length: 0
},
foo: “foo”,
funcNameA: pointer to heap memory in the function definition,
bar: “bar”
},
scopechain: [funcName execution context, Global execution context],
this: value of this
}
>
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
funcNameExecutionContextObj = {
variableObject: {
argumentObject: {
length: 0
},
foo: “foo”,
funcNameA: pointer to heap memory in the function definition,
bar: “bar”
},
scopechain: [funcName execution context, Global execution context],
this: value of this
}
globalExecutionContextObj = {
variableObject: {
argumentObject: {
length: 0
},
a: 2,
funcName: pointer to heap memory in the function definition,
b: 1,
foo1: “foo value”
},
scopechain: [Global execution context],
this: value of this
}
>
CompilationfuncNameA
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
funcNameAExecutionContextObj = {
variableObject: {
argumentObject: {
0: arg1,
length: 1
},
arg1: 10
},
scopechain: [],
this:
}
> funcNameA has arg1 as variable. JS engine will add the arg1 in the
argument object and will create the property arg1 with value 10 in
the variable object
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
funcNameAExecutionContextObj = {
variableObject: {
argumentObject: {
0: arg1,
length: 1
},
arg1: 10,
foo1: undefined
},
scopechain: [],
this:
}
> Variable shadowing
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
funcNameAExecutionContextObj = {
variableObject: {
argumentObject: {
0: arg1,
length: 1
},
arg1: 10,
foo1: undefined,
foo2: undefined
},
scopechain: [],
this:
}
>
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
funcNameAExecutionContextObj = {
variableObject: {
argumentObject: {
0: arg1,
length: 1
},
arg1: 10,
foo1: undefined,
foo2: undefined
},
scopechain: [],
this:
}
> This is not a declaration. Code won’t do anything here
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
funcNameAExecutionContextObj = {
variableObject: {
argumentObject: {
0: arg1,
length: 1
},
arg1: 10,
foo1: undefined,
foo2: undefined
},
scopechain: [],
this:
}
> This is not a declaration. Code won’t do anything here
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
funcNameAExecutionContextObj = {
variableObject: {
argumentObject: {
0: arg1,
length: 1
},
arg1: 10,
foo1: undefined,
foo2: undefined
},
scopechain: [funcNameA execution context, funcName execution context,
Global execution context],
this:
}
> end of code. Hence...
> scopechain is set
> this is set
>
> As there is no other code, JS engine will start the execution phase
ExecutionfuncNameA
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
funcNameAExecutionContextObj = {
variableObject: {
argumentObject: {
0: arg1,
length: 1
},
arg1: 10,
foo1: ”foo1”,
foo2: undefined
},
scopechain: [funcNameA execution context, funcName execution context,
Global execution context],
this:
}
>
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
funcNameAExecutionContextObj = {
variableObject: {
argumentObject: {
0: arg1,
length: 1
},
arg1: 10,
foo1: ”foo1”,
foo2: undefined
},
scopechain: [funcNameA execution context, funcName execution context,
Global execution context],
this:
}
>
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
funcNameAExecutionContextObj = {
variableObject: {
argumentObject: {
0: arg1,
length: 1
},
arg1: 10,
foo1: ”foo1”,
foo2: undefined
},
scopechain: [funcNameA execution context, funcName execution context,
Global execution context],
this:
}
globalExecutionContextObj = {
variableObject: {
argumentObject: { length: 0 },
a: 2,
funcName: pointer to heap memory in the function definition,
b: 1,
foo1: “foo value”
foo3: “foo3”
}
}
> JS engine checks if foo3 exists in funcNameA. It’s not
> JS engine checks if foo3 exists in funcName. It’s not
> JS engine checks if foo3 exists in global scope. It’s not. It
creates the variable in global scope and sets the value foo3
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
funcNameAExecutionContextObj = {
variableObject: {
argumentObject: {
0: arg1,
length: 1
},
arg1: 10,
foo1: ”foo1”,
foo2: undefined
},
scopechain: [funcNameA execution context, funcName execution context,
Global execution context],
this:
}
globalExecutionContextObj = {
variableObject: {
argumentObject: { length: 0 },
a: 4,
funcName: pointer to heap memory in the function definition,
b: 1,
foo1: “foo value”
foo3: “foo3”
}
}
> JS engine checks if a exists in funcNameA. It’s not
> JS engine checks if a exists in funcName. It’s not
> JS engine checks if a exists in global scope. It exists. It
replaces the value with 4
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
funcNameAExecutionContextObj = {
variableObject: {
argumentObject: {
0: arg1,
length: 1
},
arg1: 10,
foo1: ”foo1”,
foo2: undefined
},
scopechain: [funcNameA execution context, funcName execution context,
Global execution context],
this:
}
globalExecutionContextObj = {
variableObject: {
argumentObject: { length: 0 },
a: 4,
funcName: pointer to heap memory in the function definition,
b: 1,
foo1: “foo value”
foo3: “foo3”
}
}
> funcNameA is garbage collected
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
funcNameAExecutionContextObj = {
variableObject: {
argumentObject: {
0: arg1,
length: 1
},
arg1: 10,
foo1: ”foo1”,
foo2: undefined
},
scopechain: [funcNameA execution context, funcName execution context,
Global execution context],
this:
}
globalExecutionContextObj = {
variableObject: {
argumentObject: { length: 0 },
a: 4,
funcName: pointer to heap memory in the function definition,
b: 1,
foo1: “foo value”
foo3: “foo3”
}
}
>
1 let a = 2;
2 b = 1;
3
4 function funcName() {
5 let foo = "foo";
6
7 function funcNameA(arg1) {
8 let foo1 = "foo1";
9
10 let foo2;
11
12 foo3 = "foo3";
13
14 a = 4;
15 }
16
17 foo1 = "foo value";
18 let bar = "bar";
19
20 funcNameA(10);
21 }
22
23 funcName();
funcNameAExecutionContextObj = {
variableObject: {
argumentObject: {
0: arg1,
length: 1
},
arg1: 10,
foo1: ”foo1”,
foo2: undefined
},
scopechain: [funcNameA execution context, funcName execution context,
Global execution context],
this:
}
globalExecutionContextObj = {
variableObject: {
argumentObject: { length: 0 },
a: 4,
funcName: pointer to heap memory in the function definition,
b: 1,
foo1: “foo value”
foo3: “foo3”
}
}
> funcName is garbage collected
Variable hoisting
1 console.log(b);
2 var b = 1;
var b;
console.log(b);
b=1;
> undefined
1 console.log(c);
2 var b = 1;
var b;
console.log(c);
b=1;
> Uncaught ReferenceError: c is not defined
1 function funcA(condition) {
2 console.log(var1);
3 if (condition) {
4 var var1 = "value of var1";
5 // do something
6 }
7 }
function funcA(condition) {
var var1;
console.log(var1);
if (condition) {
var1 = "value of var1";
// do something
}
}
> undefined
Closures
1 var adder = (num) => {
2 var sum = 0;
3 return () => {
4 sum += num;
5 console.log(sum);
6 }
7 }
8
9 var adder2 = adder(2);
10 adder2(); //2
11 adder2(); //4
12 adder2(); //6
13 adder2(); //8
> a closure is a stack frame which is allocated when a function
starts its execution, and not freed after the function returns (as if
a 'stack frame' were allocated on the heap rather than the stack!).
> https://stackoverflow.com/questions/111102/how-do-javascript-closures-work
1 var classA = function () {};
2
3 function func() {
4 var funcClassA = new classA();
5
6 function unreachable() {
7 funcClassA
8 };
9 return function () {};
10 }
11
12 var funVar = func();
>
1 var classA = function () {};
2
3 function func() {
4 var funcClassA = new classA();
5
6 return function () {};
7 }
8
9 var funVar = func();
>
Fanis Prodromou
https://www.linkedin.com/in/prodromouf/
Thank you

More Related Content

What's hot

Easily mockingdependenciesinc++ 2
Easily mockingdependenciesinc++ 2Easily mockingdependenciesinc++ 2
Easily mockingdependenciesinc++ 2
drewz lin
 

What's hot (20)

Easily mockingdependenciesinc++ 2
Easily mockingdependenciesinc++ 2Easily mockingdependenciesinc++ 2
Easily mockingdependenciesinc++ 2
 
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java script
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java scriptCodiLime Tech Talk - Grzegorz Rozdzialik: What the java script
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java script
 
The Ring programming language version 1.6 book - Part 84 of 189
The Ring programming language version 1.6 book - Part 84 of 189The Ring programming language version 1.6 book - Part 84 of 189
The Ring programming language version 1.6 book - Part 84 of 189
 
The Ring programming language version 1.5.1 book - Part 74 of 180
The Ring programming language version 1.5.1 book - Part 74 of 180The Ring programming language version 1.5.1 book - Part 74 of 180
The Ring programming language version 1.5.1 book - Part 74 of 180
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 
Apache PIG - User Defined Functions
Apache PIG - User Defined FunctionsApache PIG - User Defined Functions
Apache PIG - User Defined Functions
 
The Ring programming language version 1.4.1 book - Part 22 of 31
The Ring programming language version 1.4.1 book - Part 22 of 31The Ring programming language version 1.4.1 book - Part 22 of 31
The Ring programming language version 1.4.1 book - Part 22 of 31
 
Intro to Pig UDF
Intro to Pig UDFIntro to Pig UDF
Intro to Pig UDF
 
삼성 바다 앱개발 실패 노하우 2부
삼성 바다 앱개발 실패 노하우 2부삼성 바다 앱개발 실패 노하우 2부
삼성 바다 앱개발 실패 노하우 2부
 
The Ring programming language version 1.9 book - Part 91 of 210
The Ring programming language version 1.9 book - Part 91 of 210The Ring programming language version 1.9 book - Part 91 of 210
The Ring programming language version 1.9 book - Part 91 of 210
 
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...
 
One definition rule - что это такое, и как с этим жить
One definition rule - что это такое, и как с этим житьOne definition rule - что это такое, и как с этим жить
One definition rule - что это такое, и как с этим жить
 
The Ring programming language version 1.9 book - Part 40 of 210
The Ring programming language version 1.9 book - Part 40 of 210The Ring programming language version 1.9 book - Part 40 of 210
The Ring programming language version 1.9 book - Part 40 of 210
 
4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладка4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладка
 
Scope and closures
Scope and closuresScope and closures
Scope and closures
 
The Ring programming language version 1.8 book - Part 86 of 202
The Ring programming language version 1.8 book - Part 86 of 202The Ring programming language version 1.8 book - Part 86 of 202
The Ring programming language version 1.8 book - Part 86 of 202
 
Call stack, event loop and async programming
Call stack, event loop and async programmingCall stack, event loop and async programming
Call stack, event loop and async programming
 
The Ring programming language version 1.5.2 book - Part 74 of 181
The Ring programming language version 1.5.2 book - Part 74 of 181The Ring programming language version 1.5.2 book - Part 74 of 181
The Ring programming language version 1.5.2 book - Part 74 of 181
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.
 
PyconKR 2018 Deep dive into Coroutine
PyconKR 2018 Deep dive into CoroutinePyconKR 2018 Deep dive into Coroutine
PyconKR 2018 Deep dive into Coroutine
 

Similar to Javascript compilation execution

Lexical environment in ecma 262 5
Lexical environment in ecma 262 5Lexical environment in ecma 262 5
Lexical environment in ecma 262 5
Kim Hunmin
 
The mighty js_function
The mighty js_functionThe mighty js_function
The mighty js_function
timotheeg
 
Orlando BarCamp Why Javascript Doesn't Suck
Orlando BarCamp Why Javascript Doesn't SuckOrlando BarCamp Why Javascript Doesn't Suck
Orlando BarCamp Why Javascript Doesn't Suck
erockendude
 
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
Dmitry Soshnikov
 
The Beauty Of Java Script V5a
The Beauty Of Java Script V5aThe Beauty Of Java Script V5a
The Beauty Of Java Script V5a
rajivmordani
 

Similar to Javascript compilation execution (20)

Es6 hackathon
Es6 hackathonEs6 hackathon
Es6 hackathon
 
Hello Swift 3/5 - Function
Hello Swift 3/5 - FunctionHello Swift 3/5 - Function
Hello Swift 3/5 - Function
 
Lexical environment in ecma 262 5
Lexical environment in ecma 262 5Lexical environment in ecma 262 5
Lexical environment in ecma 262 5
 
サイ本 文
サイ本 文サイ本 文
サイ本 文
 
JavaScript - i och utanför webbläsaren (2010-03-03)
JavaScript - i och utanför webbläsaren (2010-03-03)JavaScript - i och utanför webbläsaren (2010-03-03)
JavaScript - i och utanför webbläsaren (2010-03-03)
 
LISA QooxdooTutorial Slides
LISA QooxdooTutorial SlidesLISA QooxdooTutorial Slides
LISA QooxdooTutorial Slides
 
The mighty js_function
The mighty js_functionThe mighty js_function
The mighty js_function
 
Using zone.js
Using zone.jsUsing zone.js
Using zone.js
 
Workshop 1: Good practices in JavaScript
Workshop 1: Good practices in JavaScriptWorkshop 1: Good practices in JavaScript
Workshop 1: Good practices in JavaScript
 
Orlando BarCamp Why Javascript Doesn't Suck
Orlando BarCamp Why Javascript Doesn't SuckOrlando BarCamp Why Javascript Doesn't Suck
Orlando BarCamp Why Javascript Doesn't Suck
 
Check the output of the following code then recode it to eliminate fu
 Check the output of the following code then recode it to eliminate fu Check the output of the following code then recode it to eliminate fu
Check the output of the following code then recode it to eliminate fu
 
Ian 20150116 java script oop
Ian 20150116 java script oopIan 20150116 java script oop
Ian 20150116 java script oop
 
FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6
 
A topology of memory leaks on the JVM
A topology of memory leaks on the JVMA topology of memory leaks on the JVM
A topology of memory leaks on the JVM
 
Practical JavaScript Programming - Session 7/8
Practical JavaScript Programming - Session 7/8Practical JavaScript Programming - Session 7/8
Practical JavaScript Programming - Session 7/8
 
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
 
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
 
The Beauty Of Java Script V5a
The Beauty Of Java Script V5aThe Beauty Of Java Script V5a
The Beauty Of Java Script V5a
 
Object oriented JavaScript
Object oriented JavaScriptObject oriented JavaScript
Object oriented JavaScript
 
Construire une application JavaFX 8 avec gradle
Construire une application JavaFX 8 avec gradleConstruire une application JavaFX 8 avec gradle
Construire une application JavaFX 8 avec gradle
 

Recently uploaded

Recently uploaded (20)

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 

Javascript compilation execution

  • 1. JS compilation - execution
  • 2. What we will see . . . ● Compilation ● Execution ● Variable shadowing ● Variable hoisting ● Closures ● Execution context stack ● Event loop
  • 3. Compilation Phase ● Creates the variable object: Variable object is a special object in JS which contain all the variables, function arguments and inner functions declarations information. ● Creates the scope chain ● Determines the value of this Extracts all the declarations (variable, function). It prepares the memory so that it can execute the code variableObject: { argumentObject: { } }, scopechain: [], This
  • 4. Execution Phase JavaScript is a single threaded language, meaning only one task can be executed at a time. When the JavaScript interpreter initially executes code, it first enters into a global execution context by default. Each invocation of a function from this point on will result in the creation of a new execution context. ● Global execution context ● Functional execution context executionContextObj = { variableObject: { argumentObject: { } }, scopechain: [], this }
  • 6. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); globalExecutionContextObj = { variableObject: { argumentObject: { } }, scopechain: [], this } >
  • 7. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); globalExecutionContextObj = { variableObject: { argumentObject: { }, a: undefined }, scopechain: [], this } >
  • 8. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); globalExecutionContextObj = { variableObject: { argumentObject: { }, a: undefined }, scopechain: [], this } > no declaration found on this line. The JS engine moves on the next line
  • 9. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); globalExecutionContextObj = { variableObject: { argumentObject: { }, a: undefined, funcName: pointer to heap memory in the function definition }, scopechain: [], this } > Whenever the JS engines finds a function declaration, creates a property and points to heap memory where the function definition is stored > Moves to line 23
  • 10. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); globalExecutionContextObj = { variableObject: { argumentObject: { }, a: undefined, funcName: pointer to heap memory in the function definition }, scopechain: [], this } > This is not a declaration. Code won’t do anything here
  • 11. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); globalExecutionContextObj = { variableObject: { argumentObject: { length: 0 }, a: undefined, funcName: pointer to heap memory in the function definition }, scopechain: [Global execution context], this: value of this } > end of code. Hence... > scopechain is set > this is set
  • 13. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); globalExecutionContextObj = { variableObject: { argumentObject: { length: 0 }, a: 2, funcName: pointer to heap memory in the function definition }, scopechain: [Global execution context], this: value of this } >
  • 14. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); globalExecutionContextObj = { variableObject: { argumentObject: { length: 0 }, a: 2, funcName: pointer to heap memory in the function definition, b: 1 }, scopechain: [Global execution context], this: value of this } > No property with name b found. JS engine will create it and initialize it with value 1
  • 15. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); globalExecutionContextObj = { variableObject: { argumentObject: { length: 0 }, a: 2, funcName: pointer to heap memory in the function definition, b: 1 }, scopechain: [Global execution context], this: value of this } > No property with name b found. JS engine will create it and initialize it with value 1
  • 16. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); globalExecutionContextObj = { variableObject: { argumentObject: { length: 0 }, a: 2, funcName: pointer to heap memory in the function definition, b: 1 }, scopechain: [Global execution context], this: value of this } > Since it’s a function declaration the engine won’t do anything, and moves to line 23
  • 17. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); globalExecutionContextObj = { variableObject: { argumentObject: { length: 0 }, a: 2, funcName: pointer to heap memory in the function definition, b: 1 }, scopechain: [Global execution context], this: value of this } >
  • 19. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); funcNameExecutionContextObj = { variableObject: { argumentObject: {} }, scopechain: [], this: } > No arguments specified. Nothing will added in the argumentObject
  • 20. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); funcNameExecutionContextObj = { variableObject: { argumentObject: {}, foo: undefined }, scopechain: [], this: } >
  • 21. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); funcNameExecutionContextObj = { variableObject: { argumentObject: {}, foo: undefined, funcNameA: pointer to heap memory in the function definition }, scopechain: [], this: } >
  • 22. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); funcNameExecutionContextObj = { variableObject: { argumentObject: {}, foo: undefined, funcNameA: pointer to heap memory in the function definition }, scopechain: [], this: } > This is not a declaration. Code won’t do anything here
  • 23. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); funcNameExecutionContextObj = { variableObject: { argumentObject: {}, foo: undefined, funcNameA: pointer to heap memory in the function definition, bar: undefined }, scopechain: [], this: } >
  • 24. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); funcNameExecutionContextObj = { variableObject: { argumentObject: {}, foo: undefined, funcNameA: pointer to heap memory in the function definition, bar: undefined }, scopechain: [], this: } > This is not a declaration. Code won’t do anything here
  • 25. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); funcNameExecutionContextObj = { variableObject: { argumentObject: {}, foo: undefined, funcNameA: pointer to heap memory in the function definition, bar: undefined }, scopechain: [funcName execution context, Global execution context], this: value of this } > end of code. Hence... > scopechain is set > this is set > > As there is no other code, JS engine will start the execution phase
  • 27. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); funcNameExecutionContextObj = { variableObject: { argumentObject: { length: 0 }, foo: “foo”, funcNameA: pointer to heap memory in the function definition, bar: undefined }, scopechain: [funcName execution context, Global execution context], this: value of this } >
  • 28. funcNameExecutionContextObj = { variableObject: { argumentObject: { length: 0 }, foo: “foo”, funcNameA: pointer to heap memory in the function definition, bar: undefined }, scopechain: [funcName execution context, Global execution context], this: value of this } > It’s a function declaration, JS engine won’t do anything and moves to line 17 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName();
  • 29. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); funcNameExecutionContextObj = { variableObject: { argumentObject: { length: 0 }, foo: “foo”, funcNameA: pointer to heap memory in the function definition, bar: undefined }, scopechain: [funcName execution context, Global execution context], this: value of this } globalExecutionContextObj = { variableObject: { argumentObject: { length: 0 }, a: 2, funcName: pointer to heap memory in the function definition, b: 1, foo1: “foo value” }, scopechain: [Global execution context], this: value of this } > There is no foo1 variable in the execution context. JS engines asks the next execution context of the scope chain if this variable exists. It doesn’t, so it creates it and assign the value
  • 30. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); funcNameExecutionContextObj = { variableObject: { argumentObject: { length: 0 }, foo: “foo”, funcNameA: pointer to heap memory in the function definition, bar: “bar” }, scopechain: [funcName execution context, Global execution context], this: value of this } >
  • 31. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); funcNameExecutionContextObj = { variableObject: { argumentObject: { length: 0 }, foo: “foo”, funcNameA: pointer to heap memory in the function definition, bar: “bar” }, scopechain: [funcName execution context, Global execution context], this: value of this } globalExecutionContextObj = { variableObject: { argumentObject: { length: 0 }, a: 2, funcName: pointer to heap memory in the function definition, b: 1, foo1: “foo value” }, scopechain: [Global execution context], this: value of this } >
  • 33. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); funcNameAExecutionContextObj = { variableObject: { argumentObject: { 0: arg1, length: 1 }, arg1: 10 }, scopechain: [], this: } > funcNameA has arg1 as variable. JS engine will add the arg1 in the argument object and will create the property arg1 with value 10 in the variable object
  • 34. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); funcNameAExecutionContextObj = { variableObject: { argumentObject: { 0: arg1, length: 1 }, arg1: 10, foo1: undefined }, scopechain: [], this: } > Variable shadowing
  • 35. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); funcNameAExecutionContextObj = { variableObject: { argumentObject: { 0: arg1, length: 1 }, arg1: 10, foo1: undefined, foo2: undefined }, scopechain: [], this: } >
  • 36. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); funcNameAExecutionContextObj = { variableObject: { argumentObject: { 0: arg1, length: 1 }, arg1: 10, foo1: undefined, foo2: undefined }, scopechain: [], this: } > This is not a declaration. Code won’t do anything here
  • 37. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); funcNameAExecutionContextObj = { variableObject: { argumentObject: { 0: arg1, length: 1 }, arg1: 10, foo1: undefined, foo2: undefined }, scopechain: [], this: } > This is not a declaration. Code won’t do anything here
  • 38. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); funcNameAExecutionContextObj = { variableObject: { argumentObject: { 0: arg1, length: 1 }, arg1: 10, foo1: undefined, foo2: undefined }, scopechain: [funcNameA execution context, funcName execution context, Global execution context], this: } > end of code. Hence... > scopechain is set > this is set > > As there is no other code, JS engine will start the execution phase
  • 40. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); funcNameAExecutionContextObj = { variableObject: { argumentObject: { 0: arg1, length: 1 }, arg1: 10, foo1: ”foo1”, foo2: undefined }, scopechain: [funcNameA execution context, funcName execution context, Global execution context], this: } >
  • 41. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); funcNameAExecutionContextObj = { variableObject: { argumentObject: { 0: arg1, length: 1 }, arg1: 10, foo1: ”foo1”, foo2: undefined }, scopechain: [funcNameA execution context, funcName execution context, Global execution context], this: } >
  • 42. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); funcNameAExecutionContextObj = { variableObject: { argumentObject: { 0: arg1, length: 1 }, arg1: 10, foo1: ”foo1”, foo2: undefined }, scopechain: [funcNameA execution context, funcName execution context, Global execution context], this: } globalExecutionContextObj = { variableObject: { argumentObject: { length: 0 }, a: 2, funcName: pointer to heap memory in the function definition, b: 1, foo1: “foo value” foo3: “foo3” } } > JS engine checks if foo3 exists in funcNameA. It’s not > JS engine checks if foo3 exists in funcName. It’s not > JS engine checks if foo3 exists in global scope. It’s not. It creates the variable in global scope and sets the value foo3
  • 43. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); funcNameAExecutionContextObj = { variableObject: { argumentObject: { 0: arg1, length: 1 }, arg1: 10, foo1: ”foo1”, foo2: undefined }, scopechain: [funcNameA execution context, funcName execution context, Global execution context], this: } globalExecutionContextObj = { variableObject: { argumentObject: { length: 0 }, a: 4, funcName: pointer to heap memory in the function definition, b: 1, foo1: “foo value” foo3: “foo3” } } > JS engine checks if a exists in funcNameA. It’s not > JS engine checks if a exists in funcName. It’s not > JS engine checks if a exists in global scope. It exists. It replaces the value with 4
  • 44. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); funcNameAExecutionContextObj = { variableObject: { argumentObject: { 0: arg1, length: 1 }, arg1: 10, foo1: ”foo1”, foo2: undefined }, scopechain: [funcNameA execution context, funcName execution context, Global execution context], this: } globalExecutionContextObj = { variableObject: { argumentObject: { length: 0 }, a: 4, funcName: pointer to heap memory in the function definition, b: 1, foo1: “foo value” foo3: “foo3” } } > funcNameA is garbage collected
  • 45. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); funcNameAExecutionContextObj = { variableObject: { argumentObject: { 0: arg1, length: 1 }, arg1: 10, foo1: ”foo1”, foo2: undefined }, scopechain: [funcNameA execution context, funcName execution context, Global execution context], this: } globalExecutionContextObj = { variableObject: { argumentObject: { length: 0 }, a: 4, funcName: pointer to heap memory in the function definition, b: 1, foo1: “foo value” foo3: “foo3” } } >
  • 46. 1 let a = 2; 2 b = 1; 3 4 function funcName() { 5 let foo = "foo"; 6 7 function funcNameA(arg1) { 8 let foo1 = "foo1"; 9 10 let foo2; 11 12 foo3 = "foo3"; 13 14 a = 4; 15 } 16 17 foo1 = "foo value"; 18 let bar = "bar"; 19 20 funcNameA(10); 21 } 22 23 funcName(); funcNameAExecutionContextObj = { variableObject: { argumentObject: { 0: arg1, length: 1 }, arg1: 10, foo1: ”foo1”, foo2: undefined }, scopechain: [funcNameA execution context, funcName execution context, Global execution context], this: } globalExecutionContextObj = { variableObject: { argumentObject: { length: 0 }, a: 4, funcName: pointer to heap memory in the function definition, b: 1, foo1: “foo value” foo3: “foo3” } } > funcName is garbage collected
  • 48. 1 console.log(b); 2 var b = 1; var b; console.log(b); b=1; > undefined
  • 49. 1 console.log(c); 2 var b = 1; var b; console.log(c); b=1; > Uncaught ReferenceError: c is not defined
  • 50. 1 function funcA(condition) { 2 console.log(var1); 3 if (condition) { 4 var var1 = "value of var1"; 5 // do something 6 } 7 } function funcA(condition) { var var1; console.log(var1); if (condition) { var1 = "value of var1"; // do something } } > undefined
  • 52. 1 var adder = (num) => { 2 var sum = 0; 3 return () => { 4 sum += num; 5 console.log(sum); 6 } 7 } 8 9 var adder2 = adder(2); 10 adder2(); //2 11 adder2(); //4 12 adder2(); //6 13 adder2(); //8 > a closure is a stack frame which is allocated when a function starts its execution, and not freed after the function returns (as if a 'stack frame' were allocated on the heap rather than the stack!). > https://stackoverflow.com/questions/111102/how-do-javascript-closures-work
  • 53. 1 var classA = function () {}; 2 3 function func() { 4 var funcClassA = new classA(); 5 6 function unreachable() { 7 funcClassA 8 }; 9 return function () {}; 10 } 11 12 var funVar = func(); >
  • 54. 1 var classA = function () {}; 2 3 function func() { 4 var funcClassA = new classA(); 5 6 return function () {}; 7 } 8 9 var funVar = func(); >