-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy path74_this.js
103 lines (69 loc) · 5.97 KB
/
74_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// In JavaScript, this is a special keyword that refers to the object that the function is a method of.The value of this depends on how the function is called and can be different in different contexts.
// Here are some examples of how this works in different contexts:
// let person = {
// name: "John",
// sayHi: function () {
// console.log("Hi, my name is " + this.name);
// }
// };
// person.sayHi(); // Output: "Hi, my name is John"
// In this example, this refers to the person object, since sayHi is a method of the person object.When person.sayHi() is called, the this keyword inside the sayHi function refers to the person object, so the output is "Hi, my name is John".
// let sayHi = person.sayHi;
// sayHi(); // Output: "Hi, my name is undefined"
// In this example, we create a variable sayHi and assign it to the sayHi method of the person object.When we call sayHi() on its own, without specifying the object to which it belongs, the value of this is the global object, which is undefined in strict mode.Therefore, the output is "Hi, my name is undefined".
// function Person(name) {
// this.name = name;
// this.sayHi = function () {
// console.log("Hi, my name is " + this.name);
// };
// }
// let john = new Person("John");
// john.sayHi(); // Output: "Hi, my name is John"
// In this example, we define a constructor function Person that creates a new object with a name property and a sayHi method.When we create a new Person object and call its sayHi method, the value of this inside the sayHi method refers to the newly created object, so the output is "Hi, my name is John".
// In summary, the value of this in JavaScript depends on how the function is called and can be different in different contexts.It is important to understand how this works in order to write effective and maintainable JavaScript code.
// In addition to the examples I gave you earlier, here are some more ways that this can be used in JavaScript:
// function greet() {
// console.log("Hello, " + this.name);
// }
// let person1 = { name: "John" };
// let person2 = { name: "Jane" };
// greet.call(person1); // Output: "Hello, John"
// greet.call(person2); // Output: "Hello, Jane"
// In this example, we define a function greet that takes no arguments but uses this to access a name property.We then create two objects person1 and person2 with name properties.We can use the call method to call the greet function with a specific value of this.When we call greet.call(person1), the value of this inside the greet function is set to the person1 object, so the output is "Hello, John".When we call greet.call(person2), the value of this inside the greet function is set to the person2 object, so the output is "Hello, Jane".
// let button = document.querySelector("button");
// button.addEventListener("click", function () {
// console.log("Button clicked by " + this.id);
// });
// In this example, we select a button element on the page and add an event listener for the click event.Inside the event listener function, this refers to the button element that was clicked.We can use this to access properties of the button element, such as its id property, and perform actions based on the user's interaction with the button.
// In summary, this is a powerful feature of JavaScript that allows you to access properties and methods of an object dynamically, depending on how a function is called.By understanding how this works, you can write more flexible and reusable code that adapts to different contexts and situations.
// this in arrow functions:
// Arrow functions do not have their own this binding.Instead, the this value of the enclosing lexical scope(i.e., the function that contains the arrow function) is used.This can be useful in situations where you want to access the this value of an outer function inside an inner function.
// let person = {
// name: "John",
// sayHi: function () {
// let arrowFunc = () => {
// console.log("Hi, my name is " + this.name);
// };
// arrowFunc();
// }
// };
// person.sayHi(); // Output: "Hi, my name is John"
// In this example, we define an arrow function inside the sayHi method of the person object.When we call arrowFunc() inside sayHi, the value of this inside arrowFunc refers to the this value of sayHi, which is the person object.Therefore, the output is "Hi, my name is John".
// this in event handlers:
// In event handlers such as onclick or onmouseover, the value of this refers to the element that the event was triggered on.This can be useful if you want to access or modify properties of the element in response to the event.
// let button = document.querySelector("button");
// button.onclick = function () {
// console.log("Button clicked by " + this.id);
// };
// In this example, we select a button element on the page and add an onclick event handler that logs a message to the console.Inside the event handler, this refers to the button element that was clicked, so we can use this.id to access the id property of the button and log a message based on its value.
// this in constructor functions:
// When you use the new keyword to create a new object from a constructor function, the this keyword refers to the new object being created.This can be useful for setting properties and methods on the new object.
// function Person(name) {
// this.name = name;
// this.sayHi = function () {
// console.log("Hi, my name is " + this.name);
// };
// }
// let john = new Person("John");
// john.sayHi(); // Output: "Hi, my name is John"
// In this example, we define a constructor function Person that creates a new object with a name property and a sayHi method.When we create a new Person object using new Person("John"), the value of this inside the constructor function refers to the newly created object, so we can set its name property and sayHi method.When we call john.sayHi(), the value of this inside the sayHi method refers to the john object, so the output is "Hi, my name is John".