-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path18_this.js
87 lines (80 loc) · 3.13 KB
/
18_this.js
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
/**
* The `this` keyword refers to the object it belongs to, and its value can differ based on how it's used.
*/
function thisKeyword() {
/**
* ========================================================
* Global Context
* ========================================================
* In a global execution context, which is code that is outside of any function, `this` refers to the global object.
*/
console.log(this === window); // Outputs true in a browser environment
/**
* ========================================================
* Function Context
* ========================================================
* Inside a regular function, the value of `this` varies.
* In "strict mode", it is undefined. Otherwise, it's the global object.
*/
function regularFunction() {
"use strict";
console.log(this); // Outputs undefined in strict mode
}
regularFunction();
/**
* ========================================================
* Object Context
* ========================================================
* In a method, which is a function stored as an object property, `this` refers to the object.
*/
const obj = {
name: "Ishtmeet",
greet: function () {
console.log(`Hello, my name is ${this.name}`); // Outputs "Hello, my name is Ishtmeet"
},
};
obj.greet();
/**
* ========================================================
* Constructor Context
* ========================================================
* In a constructor function, `this` refers to the object that the constructor creates.
*/
function Person(name) {
this.name = name;
}
const bob = new Person("Bob");
console.log(bob.name); // Outputs "Bob"
/**
* ========================================================
* Event Handler Context
* ========================================================
* In an event handler, `this` refers to the DOM element that received the event.
*/
// HTML: <button id="myButton">Click Me</button>
document.getElementById("myButton").addEventListener("click", function () {
console.log(this.id); // Outputs "myButton"
});
/**
* ========================================================
* Explicit Binding
* ========================================================
* Using methods like `call()`, `apply()`, and `bind()`, you can explicitly set what `this` refers to.
*/
function showName(label) {
console.log(`${label}: ${this.name}`);
}
const ishtmeet = { name: "Ishtmeet" };
showName.call(ishtmeet, "User"); // Outputs "User: Ishtmeet"
/**
* ========================================================
* Arrow Functions
* ========================================================
* Arrow functions don't have their own `this`. Instead, they inherit `this` from their surrounding function context.
*/
const arrowFunc = () => {
console.log(this); // Outputs the `this` value of its surrounding function or context
};
arrowFunc();
}
thisKeyword();