Detailed explanation of the this pointing problem of JavaScript prototype objects

Detailed explanation of the this pointing problem of JavaScript prototype objects

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:

insert image description here

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 point

1. call() method

Write 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.

insert image description here

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:

insert image description here

Now this points to the newly created object o, which means the modification is successful.

2. apply() method

The 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.

Summarize

This 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:
  • Detailed explanation of JavaScript function this pointing problem
  • Detailed explanation of this pointing in JS arrow function
  • Detailed explanation of this reference and custom properties in JavaScript
  • The this keyword in JavaScript refers to

<<:  Introduction to the method attribute of the Form form in HTML

>>:  Detailed tutorial on installing harbor private warehouse using docker compose

Recommend

HTML table tag tutorial (32): cell horizontal alignment attribute ALIGN

In the horizontal direction, you can set the cell...

MySQL 5.7.17 winx64 installation and configuration tutorial

Today I installed the MySQL database on my comput...

The difference between Input's size and maxlength attributes

I recently used the input size and maxlength attri...

Analysis of the Neglected DOCTYPE Description

doctype is one of them: <!DOCTYPE HTML PUBLIC &...

How to change password in MySQL 5.7.18

How to change the password in MySQL 5.7.18: 1. Fi...

JS 9 Promise Interview Questions

Table of contents 1. Multiple .catch 2. Multiple ...

JavaScript to achieve time range effect

This article shares the specific code for JavaScr...

40 fonts recommended for famous website logos

Do you know what fonts are used in the logo desig...

Detailed example of MySQL data storage process parameters

There are three types of MySQL stored procedure p...

Detailed explanation of JS ES6 coding standards

Table of contents 1. Block scope 1.1. let replace...

How to solve jQuery conflict problem

In front-end development, $ is a function in jQue...

MySQL 8.0.12 installation and configuration method graphic tutorial (windows10)

This article records the installation graphic tut...

css add scroll to div and hide the scroll bar

CSS adds scrolling to div and hides the scroll ba...