1. DemandSuppose we have an object var person = { name: 'Frank', age: 18, phone: '13812345678', sayHi: function(){ //To be supplemented}, sayBye: function(){ //To be supplemented} } This
The request is that simple. By meeting this requirement, we can understand the essence of 2. Solutionvar person = { ... sayHi: function(name, age){ console.log('Hello, my name is ${name}, I am ${age} years old') }, sayBye: function(name, phone){ console.log('Goodbye, remember my name is ${name}, if you want to make an appointment with me, call me, my phone number is ${phone}') } } The calling method is person.sayHi(person.name, person.age) person.sayBye(person.name, person.phone) Don’t worry, I know this code is stupid, I will improve it next. 3. The first improvement In the above method, you have to select var person = { ... sayHi: function(self){ console.log('Hello, I am ${self.name}, ${self.age} years old') }, sayBye: function(self){ console.log('Goodbye, remember my name is ${self.name}, if you want to make an appointment with me, call me, my phone number is ${self.phone}') } } The calling method is person.sayHi(person) person.sayBye(person) That's a little better, but the code is still silly. 4. Add sugar The calling method most desired by developers is
The father of JS chose method 2 and used The process is as follows: // Before using this: sayHi: function(self){ console.log('Hello, I am ${self.name}, ${self.age} years old') } // After using this: sayHi: function(){ // this is self console.log('Hello, I am ${this.name}, ${this.age} years old') } 5. IncomprehensibleAfter using this, the complete code is as follows: var person = { name: 'Frank', age: 18, phone: '13812345678', sayHi: function(){ console.log('Hello, I am ${this.name}, ${this.age} years old') }, sayBye: function(){ console.log('Goodbye, remember my name is ${this.name}, if you want to make an appointment with me, call me, my phone number is ${this.phone}') } } Now it's the newbie's turn to be confused. What on earth is this
6. Problems Many JS experts disdain to use The first argument of .call is explicitly Therefore, experts usually use In this way, the parameter of That is to say, although 7. Objects and functions After Compared to This is the end of this article about why there is this in JS. For more information about why there is this in JS, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to convert rows to columns in MySQL
Table of contents Achieve results Complete code +...
Table of contents The order in which MySQL reads ...
MySQL is a relational database management system ...
Carousel animation can improve the appearance and...
This article records the installation and configu...
According to the methods of the masters, the caus...
Table of contents 1. Official Documentation 2. Cr...
This article introduces the characteristics of CS...
This article shares the specific code for impleme...
Table of contents Install Tomcat with Docker Use ...
1. What is positioning? The position attribute in...
Table of contents background What is tablespace f...
Table of contents 1. Embed CSS styles directly in...
Overview In actual business scenario applications...
Tips for sending HTML emails: Use style to write ...