Skip to content

Commit 2dd613b

Browse files
committed
this keyword further use-case
1 parent fcb0ab4 commit 2dd613b

File tree

4 files changed

+127
-43
lines changed

4 files changed

+127
-43
lines changed

Javascript/Prototypal-Inheritence/ES6-Function-definition-With-Class.js

+4
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@ var w1 = new Worker("Paul");
3232

3333
p1.hello(); // => Hello Rohan you are a person
3434
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.
38+
*/

Javascript/js-basics/call-apply-bind-this-keyword.js

+37-16
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
1-
let obj1 = { name: "Rohan" }
1+
// ***********************EXAMPLE - 1 **************************************//
2+
/* 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.
23
3-
const greeting = 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+
function add(c, d) {
10+
return this.a + this.b + c + d;
511
}
612

13+
var o = { 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
23+
add.apply(o, [10, 20]); // 34
24+
// ***********************EXAMPLE - 2 **************************************//
25+
26+
let obj1 = { name: "Rohan" };
27+
28+
const greeting = function(a, b) {
29+
return `welcome ${this.name} to ${a}, ${b}`;
30+
};
31+
732
// console.log(greeting.call(obj1, "Hitech City", "Hyd"));
833

9-
let obj2 = { name: "Steve" }
34+
let obj2 = { name: "Steve" };
1035

1136
// console.log(greeting.call(obj2, "Hitech City", "Hyd"));
1237

@@ -21,18 +46,18 @@ D> When it is inside of an object’s method — the function’s owner is t
2146
2247
*/
2348

24-
// ***********************EXAMPLE - 2**************************************//
49+
// ***********************EXAMPLE - 3 **************************************//
2550

2651
var car = {
27-
regNo : 'GA123',
28-
brand: 'Ford',
52+
regNo: "GA123",
53+
brand: "Ford",
2954

30-
displayDetails: function (ownerName) {
55+
displayDetails: function(ownerName) {
3156
console.log(`${ownerName}, this is your car : ${this.regNo} ${this.brand}`);
3257
}
33-
}
58+
};
3459
// Now invoking the function normally will result
35-
car.displayDetails("Rohan")
60+
car.displayDetails("Rohan");
3661

3762
/* 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
3863
@@ -44,16 +69,12 @@ When inside an object literal, every functions 'this' is scoped to the object, a
4469

4570
// 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? -
4671

47-
var myCarDetails1 = car.displayDetails;
72+
var myCarDetails1 = car.displayDetails;
4873
// myCarDetails1();
4974

5075
/* 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. */
5176

52-
var myCarDetails2 = car.displayDetails.bind(car, 'Steve');
77+
var myCarDetails2 = car.displayDetails.bind(car, "Steve");
5378
myCarDetails2();
5479

5580
// The bind() method creates a new function where “this” refers to the first argument or parameter in the parenthesis in the above case “car”.
56-
57-
58-
59-

Javascript/js-basics/closure-setTimeout.js

+7-12
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,21 @@ C) Explanation why the time interval is taking 1 second for consoling out the la
2222
/* SOLUTION -
2323
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. */
2424

25-
funcToExecute = (x) => {
26-
return () => {
27-
console.log((x));
28-
}
29-
}
25+
funcToExecute = x => {
26+
return () => {
27+
console.log(x);
28+
};
29+
};
3030

3131
for (var i = 1; i <= 5; i++) {
32-
33-
setTimeout(funcToExecute(i), i * 500)
34-
35-
36-
32+
setTimeout(funcToExecute(i), i * 500);
3733
}
3834

39-
4035
/* More Explanation on why the delay is happening in the correct solution and not in the original solution -
4136
4237
In JavaScript you only have 2 ways of passing an argument....pass by value or pass by reference.
4338
4439
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.
4540
4641
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.
47-
*/
42+
*/

Javascript/js-basics/this-keyword.js

+79-15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,72 @@
1-
/* FIRST TEST CASE -
1+
/* 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+
function f1() {
8+
return this;
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+
function f2() {
20+
"use strict"; // see strict mode
21+
return this;
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+
var obj = { a: "Custom" };
32+
33+
// This property is set on the global object
34+
var a = "Global";
35+
36+
function whatsThis() {
37+
return this.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+
function add(c, d) {
47+
return this.a + this.b + c + d;
48+
}
49+
50+
var o = { 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
60+
add.apply(o, [10, 20]); // 34
61+
62+
// *************************************************************
63+
/* MORE TEST CASE - Case-1
264
A) Inside a function, the value of this depends on how the function is called.
365
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
466
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
567
6-
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”.
770
*/
871

972
global.a = 2;
@@ -12,7 +75,7 @@ function foo() {
1275
return this.a;
1376
}
1477

15-
console.log(foo()); // => 2
78+
console.log(foo()); // => 2
1679

1780
// SECOND TEST CASE
1881

@@ -22,32 +85,33 @@ function foo1() {
2285
console.log(this.b);
2386
}
2487

25-
foo1(); // => 'undefined'
88+
foo1(); // => 'undefined'
2689

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
2791

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. */
2994

3095
var obj = {
31-
c : 3,
96+
c: 3,
3297
foo3: foo3
33-
}
98+
};
3499

35-
function foo3 () {
100+
function foo3() {
36101
console.log(this.c);
37102
}
38-
obj.foo3() // => 3
103+
obj.foo3(); // => 3
39104

40105
// FOURTH TEST CASE
41-
function foo4 () {
106+
function foo4() {
42107
console.log(this === global);
43108
}
44-
foo4() // => true
45-
109+
foo4(); // => true
46110

47111
// 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.
48112

49-
function foo5 () {
50-
'use strict'
113+
function foo5() {
114+
"use strict";
51115
console.log(this === global);
52116
}
53-
foo5() // => false
117+
foo5(); // => false

0 commit comments

Comments
 (0)