1. this points to This in the constructor refers to the instance object. So what does the prototype object this point to? as follows: function Student(age,name){ this.age = age; this.name = name; } var that; Student.prototype.score = function(){ console.log('The children all have good grades!'); that = this; } var xl = new Student(18,'Little Bear'); xl.score(); console.log(that === xl); Define a global variable that, assign a value to it in the score function, let it point to this in the function, call the score method of the instance object, and determine whether that and the instance object are consistent. If they are consistent, it means that the prototype object this points to the instance. The print result is: That is, the prototype object contains the method, and this in the method refers to the caller of the method, that is, the instance object. 2. Modify this point1. call() methodWrite a simple function to add two numbers. function fn(a,b){ console.log(a+b); console.log(this); } fn(1,2) Print this inside the function, call the function, and see where its this points to. It can be seen that this points to the window object. If we want this to point to a newly created object, how do we do it? First define an object. o = {}; Then modify this point through call() to point to the newly created object o o = { name: 'xl' }; fn.call(o,1,2); The print result is: Now this points to the newly created object o, which means the modification is successful. 2. apply() methodThe apply() and call() methods are basically the same, so I won’t go into details here. Let’s go straight to the code: function fn(a,b){ console.log(a+b); console.log(this); } o = { name: 'xl' }; fn.apply(o,[1,2]); It can be found that there are still differences between these two methods, namely: the parameters accepted by call() must be continuous parameters, while the parameters received by the apply() method are in the form of array parameters. SummarizeThis article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Introduction to the method attribute of the Form form in HTML
>>: Detailed tutorial on installing harbor private warehouse using docker compose
Vue3 project encapsulation side navigation text s...
Today, when I was using Nginx, a 500 error occurr...
Table of contents 1. Rendering 2. Implementation ...
Preface I believe everyone is familiar with addin...
MySQL and connection related timeouts Preface: To...
Preface Believe me, as long as you remember the 7...
Table of contents Achieve results Introduction to...
1. Postgres database backup in Docker Order: dock...
Preface: position:sticky is a new attribute of CS...
Loading rules of require method Prioritize loadin...
This article example shares the specific code of ...
Table of contents Implementation ideas: Step 1: C...
Two small problems, but they bothered me for a lon...
Without further ado, I will post the code for you...
Table of contents Preface 1. Nginx installation 1...