JavaScript in-depth analysis of the direction of this and how to modify the direction

JavaScript in-depth analysis of the direction of this and how to modify the direction

this

As usual, let’s look at the code first:

Method

function test(){
    console.log(this);
}

insert image description here

In the object

Person={
  name:"Zhang San",
  eat:function(){
      console.log(this)
  }
}

insert image description here

In a method, this refers to the object to which the method belongs. Because the first one is a method on window, window is printed, and the eat method is a Person method, so the object Person is printed.

So it can be seen that using this alone in the console represents the global object.

insert image description here

Hidden this

In objects, you can declare them one by one in advance:

var Person1 = {
    name:"Zhang San",
    age:18
}
var Person2 = {
    name:"Li Si",
    age:19
}

This would be very troublesome to write, so you can learn from the concept of Java class, like this:

var Person = function (name, age) {
    this.name=name,
    this.age=age
       
}
var Person1=new Person("张三",18);
var Person2=new Person("Li Si",19);

insert image description here

In fact, there is a return this hidden in new. If you don’t use new, you will find that it does not return the newly created object.

insert image description here

Now let’s complete it:

var Person = function (name, age) {
    this.name=name,
    this.age=age
    return this;
}
var Person1=new Person("张三",18);
var Person2=new Person("Li Si",19);

insert image description here

In this way, you can even fake the effect of this:

var Person = function (name, age) {
    var that={};
    that.name=name,
    that.age=age
    return that;
}
var Person1=new Person("张三",18);
var Person2=new Person("Li Si",19);

insert image description here

Strict Mode

This has some magical behavior in strict mode and non-strict mode

function test() {
  return this;
}

# If "use strict" is added before js, it means strict mode "use strict";
function test() {
  return this;
}

insert image description here

This shows that in a function in non-strict mode, the owner of the function is bound to this by default. So the global value can be printed, but in strict mode the function is not bound to this, so this is undefined.

You can change this to point to

Look at the code first

var Person = function (name, age) {
    this.name=name,
    this.age=age,
    this.fun = function(){
         console.log("print",this.name);
        }
}
var Person1=new Person("张三",18);
var Person2=new Person("Li Si",19);




insert image description here

It can be seen that this points to the value of a in window rather than the value in method test, but some keywords can modify the pointer.

insert image description here

You can see who the object before the method is, and who the this in the call is, but it can be modified, such as using the keywords call, apply, and bind.

insert image description here

insert image description here

insert image description here

From the above, we can see that call and apply are very similar, but bind does not execute the function immediately and needs to be enclosed in ().

However, if you bring parameters, you will find that call and apply are still different, but both of them must contain objects. After all, this points to the object.

Keywords Direct method call parameter
call Will automatically run the method used Both can have parameters in the following format: obj1.obj1Fun.call( obj2, parameter 1, parameter 1 ………………);
apply Will automatically run the method used Both can take parameters. The format is as follows: obj1.obj1Fun.apply([obj2, parameter 1, parameter 1 ………………]); Compared with the parameters of call, its parameters are enclosed in [].
bind The method will not be automatically run, you need to add + () to call it. Both can take parameters in the following format: Both can take parameters in the following format: obj1.obj1Fun.bind( [obj2, parameter 1, parameter 1 ………………])(); The parameters of apply are the same, except that it needs to be followed by +() to be called

This concludes this article on in-depth analysis of the direction of this in JavaScript and how to modify the direction. For more relevant JavaScript this direction content, 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:
  • Detailed explanation of this reference and custom properties in JavaScript
  • Detailed explanation of the this pointing problem in JavaScript
  • Detailed explanation of function classification and examples of this pointing in Javascript
  • Detailed explanation of this pointing problem in JavaScript function
  • JavaScript basics of this pointing

<<:  IDEA complete code to connect to MySQL database and perform query operations

>>:  Application of CSS3 animation effects in activity pages

Recommend

Simple implementation of Mysql add, delete, modify and query statements

Simple implementation of Mysql add, delete, modif...

Steps and pitfalls of upgrading linux mysql5.5 to mysql5.7

Table of contents Linux MySQL 5.5 upgraded to MyS...

Explanation of building graph database neo4j in Linux environment

Neo4j (one of the Nosql) is a high-performance gr...

JS achieves five-star praise case

This article shares the specific code of JS to ac...

vue+echarts realizes the flow effect of China map (detailed steps)

@vue+echarts realizes the flow effect of China ma...

Teach you how to make cool barcode effects

statement : This article teaches you how to imple...

Vue+ssh framework to realize online chat

This article shares the specific code of Vue+ssh ...

A brief discussion on MySQL temporary tables and derived tables

About derived tables When the main query contains...

Detailed explanation of styles in uni-app

Table of contents Styles in uni-app Summarize Sty...

How to solve the 2002 error when installing MySQL database on Alibaba Cloud

The following error occurred while installing the...