This is a keyword in js. The value of this will change depending on the use of the function. But there is always a principle, that is, this refers to the object that calls the function.
1. Pure function calls.
function test() { this.x = 1; alert(x);}test();In fact, this is a global variable. You can understand well by looking at the following examples, in fact, this is the global object Global. In fact, this is a global variable. You can understand well by looking at the following examples, in fact, this is the global object Global.
var x = 1;function test() { alert(this.x);}test();//1var x = 1;function test() { this.x = 0;}test();alert(x);//02. As a method call, this refers to this superior object.
function test() { alert(this.x);}var o = {};ox = 1;om = test;om(); //13. Called as a constructor. The so-called constructor is to generate a new object. At this time, this refers to this object.
function test() { this.x = 1;}var o = new test();alert(ox);//14. Apply call
var x = 0;function test() { alert(this.x);}var o = {};ox = 1;om = test;omapply(); //0o.m.apply(o);//1When apply has no parameters, it is represented as a global object. So the value is 0.
The above article deeply understands the usage of this in js. This is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.