-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathThis-Keyword.js
More file actions
167 lines (137 loc) Β· 4.37 KB
/
Copy pathThis-Keyword.js
File metadata and controls
167 lines (137 loc) Β· 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* π‘"JavaScript-with-JC"
π "this" keyword in deep
In Javascript, "this" depends on the context on which "this" is currently in. this keyword refers to different objects depending on how it is used.
π‘ 7 Things you should know about "this"
π Rule 1) function with new keyword ( this refers to the function object )
π Rule 2) call, apply, bind ( this refers to the obj passed to methods )
π Rule 3) method in object ( this refers to the object )
π Rule 4) simple function ( this refers to window object, undefined in strict mode )
π Rule 5) multiple rules ( Higher rule has given priority )
π Rule 6) arrow function ( inherits "this" from its outer function )
π Miscellaneous important things about "this"
*/
// π‘ 6 Rules of "this"
// Rule 1) function with new keyword ( this refers to the function object )
function Person() {
console.log(this); // Person {}
this.name = "Jayesh";
this.age = 24;
console.log(this); // Person { name: 'Jayesh', age: 24 }
}
new Person();
// Rule 2) call, apply, bind ( this refers to the obj passed to methods)
const player = {
name: "Virat",
role: "Batsman",
};
// Note:- Normal function ( not an arrow function )
const getPlayerInfo = function (country) {
console.log(this); // { name: 'Virat', role: 'Batsman' }
console.log(this.name, this.role, country); // Virat Batsman India
};
getPlayerInfo.call(player, "India"); // here, player borrowing getPlayerInfo function
// Rule 3) method in object ( this refers to the object )
const obj = {
name: "Jc",
displayName() {
console.log(this); // {name: 'Jc', displayName: Ζ}
console.log(this.name); // Jc
},
};
obj.displayName();
// Rule 4) simple function ( undefined in strict mode)
function simpleFunc() {
console.log(this); // window object
}
simpleFunc(); // or window.simpleFunc()
// Rule 5) multiple rules :- Higher rule has more priority
const obj1 = {
name: "Jayesh",
showName() {
console.log(this.name); // Jc
},
};
const obj2 = {
name: "Jc",
};
obj1.showName.call(obj2); // Jc precedence of Rule 2) Call method > Rule 3) method in object
// Rule 6) arrow function :- arrow function does not create its own execution context
// inherits "this" from its outer function.
// case 1:- arrow function without any parent function
const arrowFunc = () => {
// ( Global function's "this" is window object, arrowFunc inherits "this" of Global function in this case )
console.log(this); // window object
};
arrowFunc();
// case 2:- arrow function inside normal function
const animal = {
name: "cat",
displayName() {
console.log(this); // {name: 'cat', displayName: Ζ}
console.log(this.name); // cat
// inner arrow function inherits "this" from its outer normal funtion
const innerArrow = () => {
console.log(this); // {name: 'cat', displayName: Ζ}
console.log(this.name); // cat
};
innerArrow();
},
};
animal.displayName();
// case 3 :- arrow function inside an arrow function
const outerArrow = () => {
console.log(this); // window object ( Global function's "this" is window object )
// inner arrow function inherits "this" from its outer funtion
const innerArrow = () => {
console.log(this); // window object
};
innerArrow();
};
outerArrow();
// case 4:- arrow function inside function constructor ( new keyword )
function OuterFunction() {
this.name = "jayeh";
console.log(this); // OuterFunction { name: 'jayeh' }
// inner arrow function inherits "this" from its outer funtion
const innerArrow = () => {
console.log(this); // OuterFunction { name: 'jayeh' }
};
innerArrow();
}
new OuterFunction();
// π‘ Miscellaneous important things about "this"
// "this" inside nested normal function
const myObj = {
name: "Jc",
outerNormal() {
console.log(this); // {name: 'Jc', outerNormal: Ζ}
function innerNormal() {
console.log(this); // window object
}
innerNormal();
},
};
myObj.outerNormal();
// "this" inside normal function having outer arrow function
const myobj2 = {
name: "Jc",
outerArrowFoo: () => {
console.log(this); // window object
function innerNormal() {
console.log(this); // window object
}
innerNormal();
},
};
myobj2.outerArrowFoo();
// "this" inside nested objects
const outerObj = {
name: "Jc",
innerObj: {
name: "inner Jc",
getName() {
console.log(this.name); // inner Jc
},
},
};
outerObj.innerObj.getName();