You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Javascript/Prototypal-Inheritence/ES6-Function-definition-With-Class.js
+4
Original file line number
Diff line number
Diff line change
@@ -32,3 +32,7 @@ var w1 = new Worker("Paul");
32
32
33
33
p1.hello();// => Hello Rohan you are a person
34
34
w1.hello();// => Hello Paul you are a Developer
35
+
36
+
/*
37
+
In line 19, the super keyword is used as a “function” which calls the parent class Person with the parameters passed to Worker. This is a key step to be carried out in order to make sure that Worker is an instance of Person.
/* First note the definition of 'this' - By the official doc (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this) - 'this' is just The JavaScript context object in which the current code is executing. In the global execution context (outside of any function), this refers to the global object whether in strict mode or not.
2
3
3
-
constgreeting=function(a,b){
4
-
return`welcome ${this.name} to ${a}, ${b}`
4
+
In the global execution context (outside of any function), this refers to the global object whether in strict mode or not.
5
+
*/
6
+
7
+
/* Where a function uses the this keyword in its body, its value can be bound to a particular object in the call using the call() or apply() methods which all functions inherit from Function.prototype. */
8
+
9
+
functionadd(c,d){
10
+
returnthis.a+this.b+c+d;
5
11
}
6
12
13
+
varo={a: 1,b: 3};
14
+
15
+
// The first parameter is the object to use as
16
+
// 'this', subsequent parameters are passed as
17
+
// arguments in the function call
18
+
add.call(o,5,7);// 16
19
+
20
+
// The first parameter is the object to use as
21
+
// 'this', the second is an array whose
22
+
// members are used as the arguments in the function call
console.log(`${ownerName}, this is your car : ${this.regNo}${this.brand}`);
32
57
}
33
-
}
58
+
};
34
59
// Now invoking the function normally will result
35
-
car.displayDetails("Rohan")
60
+
car.displayDetails("Rohan");
36
61
37
62
/* Again in the above, I can not use arrow function, as this.regNo will refer to the global 'this' where it does not have a regNo. See this - https://github.com/babel/babel/issues/1742
38
63
@@ -44,16 +69,12 @@ When inside an object literal, every functions 'this' is scoped to the object, a
44
69
45
70
// Coming back to displayDetails() function - But what if we want to borrow a method, like below. Or What if we would like to pass a parameter to the displayDetails function? -
46
71
47
-
varmyCarDetails1=car.displayDetails;
72
+
varmyCarDetails1=car.displayDetails;
48
73
// myCarDetails1();
49
74
50
75
/* Well, this won’t work as the “this” will be now assigned to the global context which doesn’t have neither the regNo or the brand property. So, to do that, I have to user bind. */
Copy file name to clipboardExpand all lines: Javascript/js-basics/closure-setTimeout.js
+7-12
Original file line number
Diff line number
Diff line change
@@ -22,26 +22,21 @@ C) Explanation why the time interval is taking 1 second for consoling out the la
22
22
/* SOLUTION -
23
23
To print the successive values of i with an interval of 1 second, we need to pass into setTimeout() the actual value of i at the moment of each loop execution in the for statement. */
24
24
25
-
funcToExecute=(x)=>{
26
-
return()=>{
27
-
console.log((x));
28
-
}
29
-
}
25
+
funcToExecute=x=>{
26
+
return()=>{
27
+
console.log(x);
28
+
};
29
+
};
30
30
31
31
for(vari=1;i<=5;i++){
32
-
33
-
setTimeout(funcToExecute(i),i*500)
34
-
35
-
36
-
32
+
setTimeout(funcToExecute(i),i*500);
37
33
}
38
34
39
-
40
35
/* More Explanation on why the delay is happening in the correct solution and not in the original solution -
41
36
42
37
In JavaScript you only have 2 ways of passing an argument....pass by value or pass by reference.
43
38
44
39
In the incorrect solution above in the loop, i is being passed by reference. So the loop is done by the time first console.log runs and and i is already at 6. To pass by reference in JavaScript, the argument has to be object.property. However in the loop case above, this is a exception where a variable is being passed as a reference to the value instead of the value itself.
45
40
46
41
Now, other than the loop exception.... if you pass a variable as a arg into a function(x).... you are passing by value. Thus, passing the variable i in the function(x) passes by value.
/* By the official doc (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this) - 'this' is just The JavaScript context object in which the current code is executing. In the global execution context (outside of any function), this refers to the global object whether in strict mode or not.
2
+
3
+
In the global execution context (outside of any function), this refers to the global object whether in strict mode or not.
4
+
*/
5
+
// Inside a function, the value of this depends on how the function is called.
6
+
// Case-1 - WITHOUT STRICT MODE - Since the following code is not in strict mode, and because the value of this is not set by the call, this will default to the global object, which is window in a browser.
7
+
functionf1(){
8
+
returnthis;
9
+
}
10
+
11
+
// In a browser:
12
+
f1()===window;// true
13
+
14
+
// In Node:
15
+
f1()===global;// true
16
+
17
+
// Case-2 - In strict mode, however, the value of this remains at whatever it was set to when entering the execution context, so, in the following case, this will default to undefined. So, in strict mode, if this was not defined by the execution context, it remains undefined. So, in strict mode, if this was not defined by the execution context, it remains undefined.
18
+
19
+
functionf2(){
20
+
"use strict";// see strict mode
21
+
returnthis;
22
+
}
23
+
24
+
f2()===undefined;// true
25
+
26
+
// In the Case-2 above, `this` was returned to be undefined, because f2 was called directly and not as a method or property of an object (e.g. window.f2()).
27
+
28
+
// To pass the value of this from one context to another, use call(), or apply():
29
+
30
+
// An object can be passed as the first argument to call or apply and this will be bound to it.
31
+
varobj={a: "Custom"};
32
+
33
+
// This property is set on the global object
34
+
vara="Global";
35
+
36
+
functionwhatsThis(){
37
+
returnthis.a;// The value of this is dependent on how the function is called
38
+
}
39
+
40
+
whatsThis();// 'Global'
41
+
whatsThis.call(obj);// 'Custom'
42
+
whatsThis.apply(obj);// 'Custom'
43
+
44
+
/* Where a function uses the 'this' keyword in its body, its value can be bound to a particular object in the call using the call() or apply() methods which all functions inherit from Function.prototype. */
45
+
46
+
functionadd(c,d){
47
+
returnthis.a+this.b+c+d;
48
+
}
49
+
50
+
varo={a: 1,b: 3};
51
+
52
+
// The first parameter is the object to use as
53
+
// 'this', subsequent parameters are passed as
54
+
// arguments in the function call
55
+
add.call(o,5,7);// 16
56
+
57
+
// The first parameter is the object to use as
58
+
// 'this', the second is an array whose
59
+
// members are used as the arguments in the function call
A) Inside a function, the value of this depends on how the function is called.
3
65
Since the following code is not in strict mode, and because the value of this is not set by the call, this will default to the global object, which is window in a browser. And global in node
B) By default the execution context for an execution is global which means that if a code is being executed as part of a simple function call then “this” refers to global object. “window” object is the global object in case of browser and in Node.JS environment, a special object “global” will be the value of “this”.
68
+
B) By default the execution context for an execution is global which means that if a code is being executed as part of a simple function call then “this” refers to global object.
69
+
“window” object is the global object in case of browser and in Node.JS environment, a special object “global” will be the value of “this”.
7
70
*/
8
71
9
72
global.a=2;
@@ -12,7 +75,7 @@ function foo() {
12
75
returnthis.a;
13
76
}
14
77
15
-
console.log(foo());// => 2
78
+
console.log(foo());// => 2
16
79
17
80
// SECOND TEST CASE
18
81
@@ -22,32 +85,33 @@ function foo1() {
22
85
console.log(this.b);
23
86
}
24
87
25
-
foo1();// => 'undefined'
88
+
foo1();// => 'undefined'
26
89
90
+
// Explanation of the above - I got 'undefined' because in VS-Code here, I am in node environment so just declaring var b = 2 will not attach the variable to the 'global' environment of vs-code
27
91
28
-
// THIRD TEST CASE - “this” refers to invoker object (parent object). When an Object’s method is invoked then “this” refers to the object which contains the method being invoked.
92
+
/* THIRD TEST CASE - “this” refers to invoker object (parent object).
93
+
When an Object’s method is invoked then “this” refers to the object which contains the method being invoked. */
29
94
30
95
varobj={
31
-
c: 3,
96
+
c: 3,
32
97
foo3: foo3
33
-
}
98
+
};
34
99
35
-
functionfoo3(){
100
+
functionfoo3(){
36
101
console.log(this.c);
37
102
}
38
-
obj.foo3()// => 3
103
+
obj.foo3();// => 3
39
104
40
105
// FOURTH TEST CASE
41
-
functionfoo4(){
106
+
functionfoo4(){
42
107
console.log(this===global);
43
108
}
44
-
foo4()// => true
45
-
109
+
foo4();// => true
46
110
47
111
// FIFTH TEST CASE - If strict mode is enabled for any function then the value of “this” will be “undefined” as in strict mode, global object refers to undefined in place of windows object.
0 commit comments