SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances

Easily mockingdependenciesinc++ 2
Easily mockingdependenciesinc++ 2Easily mockingdependenciesinc++ 2
Easily mockingdependenciesinc++ 2drewz lin
 
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 scriptCodiLime
 
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 189Mahmoud Samir Fayed
 
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 180Mahmoud Samir Fayed
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2zindadili
 
Apache PIG - User Defined Functions
Apache PIG - User Defined FunctionsApache PIG - User Defined Functions
Apache PIG - User Defined FunctionsChristoph Bauer
 
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 31Mahmoud Samir Fayed
 
삼성 바다 앱개발 실패 노하우 2부
삼성 바다 앱개발 실패 노하우 2부삼성 바다 앱개발 실패 노하우 2부
삼성 바다 앱개발 실패 노하우 2부mosaicnet
 
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 210Mahmoud Samir Fayed
 
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...Sergey Platonov
 
One definition rule - что это такое, и как с этим жить
One definition rule - что это такое, и как с этим житьOne definition rule - что это такое, и как с этим жить
One definition rule - что это такое, и как с этим житьPlatonov Sergey
 
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 210Mahmoud Samir Fayed
 
4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладка4. Обработка ошибок, исключения, отладка
4. Обработка ошибок, исключения, отладкаDEVTYPE
 
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 202Mahmoud Samir Fayed
 
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 programmingMasters Academy
 
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 181Mahmoud Samir Fayed
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.Russell Childs
 
PyconKR 2018 Deep dive into Coroutine
PyconKR 2018 Deep dive into CoroutinePyconKR 2018 Deep dive into Coroutine
PyconKR 2018 Deep dive into CoroutineDaehee Kim
 

Tendances (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
 

Similaire à Javascript compilation execution

Hello Swift 3/5 - Function
Hello Swift 3/5 - FunctionHello Swift 3/5 - Function
Hello Swift 3/5 - FunctionCody Yun
 
Lexical environment in ecma 262 5
Lexical environment in ecma 262 5Lexical environment in ecma 262 5
Lexical environment in ecma 262 5Kim Hunmin
 
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)Anders Jönsson
 
LISA QooxdooTutorial Slides
LISA QooxdooTutorial SlidesLISA QooxdooTutorial Slides
LISA QooxdooTutorial SlidesTobias Oetiker
 
The mighty js_function
The mighty js_functionThe mighty js_function
The mighty js_functiontimotheeg
 
Workshop 1: Good practices in JavaScript
Workshop 1: Good practices in JavaScriptWorkshop 1: Good practices in JavaScript
Workshop 1: Good practices in JavaScriptVisual Engineering
 
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 Suckerockendude
 
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 fulicservernoida
 
Ian 20150116 java script oop
Ian 20150116 java script oopIan 20150116 java script oop
Ian 20150116 java script oopLearningTech
 
FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6Dmitry Soshnikov
 
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 JVMRafael Winterhalter
 
Practical JavaScript Programming - Session 7/8
Practical JavaScript Programming - Session 7/8Practical JavaScript Programming - Session 7/8
Practical JavaScript Programming - Session 7/8Wilson Su
 
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"OdessaJS Conf
 
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 6Dmitry Soshnikov
 
The Beauty Of Java Script V5a
The Beauty Of Java Script V5aThe Beauty Of Java Script V5a
The Beauty Of Java Script V5arajivmordani
 
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 gradleThierry Wasylczenko
 

Similaire à 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
 

Dernier

The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 

Dernier (20)

The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 

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(); >