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: 15_this_keyword/1_this_keyword.js
+11-21Lines changed: 11 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,10 @@
1
1
// `this` Keyword
2
2
3
3
// What
4
-
// - "this" keyword refers to an object that is executing the current piece of code. It references the object that is executing the current function.
4
+
// - `this` refers to the object that "owns" the currently executing code.
5
5
6
6
// Note
7
-
// The this keyword refers to different objects depending on how it is used:
8
-
9
-
// 1. this -> Alone -> Global Object
10
-
11
-
// 2. this -> In Regular Function -> Global Object/ Strict Mode: Undefine
12
-
// 3. this -> Regular Function Inside Regular Function -> Global Object/ Strict Mode: Undefine
13
-
// 4. this -> In Object Method (Method is Regular Function) -> Current Object
7
+
// this == Current Context (loosely say: Current Object)
14
8
15
9
// 5. this -> In Arrow Function -> Global Object
16
10
// 6. this -> Arrow Function Inside Arrow Function -> Global object
@@ -19,21 +13,18 @@
19
13
// TODO: In an event, this refers to the element that received the event.
20
14
// TODO: Methods like call(), apply(), and bind() can refer this to any object.
21
15
22
-
// 1. this -> Alone -> Global Object
23
-
// - If `this` keyword use in global scope it will refer to the global object.
16
+
// this -> Alone -> Global Object
24
17
console.log(this);
25
18
26
-
// REGULAR FUNCTION & `THIS` KEYWORD
27
-
28
-
// 2. this -> In Regular Function -> Global Object
29
-
// - If `this` keyword use inside the regular function it will refer to global object.
19
+
// this -> Unstrict Mode -> In Regular Function -> Global Object
20
+
// this -> Strict Mode -> In Regular Function -> Undefined
30
21
functionregularFunction(){
31
22
returnconsole.log(this);
32
23
}
33
24
console.log(regularFunction());
34
25
35
-
// 3. this -> Regular Function Inside Regular Function -> Global Object
36
-
// - If `this` keyword use inside the regular function. Which also wrap with the regular function. It will refer to global object.
26
+
// this -> Unstrict Mode -> Regular Function Inside Regular Function -> Global Object
27
+
// this -> Strict Mode -> Regular Function Inside Regular Function -> Undefined
37
28
functionouterRegularFunction(){
38
29
functioninnerRegularFunction(){
39
30
returnconsole.log(this);
@@ -42,8 +33,7 @@ function outerRegularFunction() {
42
33
}
43
34
console.log(outerRegularFunction());
44
35
45
-
// 4. this -> In Object Method (Method is Regular Function) -> Current Object
46
-
// - If `this` keyword use inside the object's method which is regular function it will refer to the object it self.
36
+
// this -> Unstrict/ Strict Mode -> Object's Method -> Current Object
47
37
constobject={
48
38
key: "value",
49
39
method: function(){
@@ -56,7 +46,9 @@ console.log(object.method());
56
46
// ARROW FUNCTION & `THIS` KEYWORD
57
47
58
48
// Note
59
-
// - Arrow functions are a special type of function in JavaScript. One key characteristic of arrow functions is that they do not have their own this context. Instead, they inherit this from the surrounding code where they are defined.
49
+
// -> Arrow functions are a special type of function in JavaScript.
50
+
// -> One key characteristic of arrow functions is that they do not have their own this context.
51
+
// -> Instead, they inherit this from the surrounding code where they are defined.
60
52
61
53
// 5. this -> In Arrow Function -> Global Object
62
54
// - If `this` keyword use inside the arrow function it will refer to global object.
@@ -106,5 +98,3 @@ function Person(name) {
106
98
}
107
99
constperson1=newPerson("Alice");
108
100
console.log(person1.name);// this refers to the new Person instance, person1.
0 commit comments