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
Centos7 uses yum to install MySQL and how to achi...
Table of contents Two major categories of functio...
Table of contents Component Design Defining the f...
When applying docker containers, we often mount t...
Solution Add position:relative to the parent elem...
In fact, it is not difficult to build an Apache c...
I came into contact with CSS a long time ago, but...
JS provides three methods for intercepting string...
Detailed explanation of HTML (select option) in ja...
Find the problem When we display the contents in ...
1. Fixed width + adaptive Expected effect: fixed ...
1. Introduction I won’t go into details about apo...
MySQL 5.7.8 and later began to support a native J...
What is keepalive? In normal development, some co...
When using the docker-maven-plugin plug-in, Maven...