Home >Web Front-end >JS Tutorial >A brief introduction to this variable in JS
This variable in JavaScript is an elusive keyword. This is very powerful. In this article, we will introduce the use of this in JavaScript in detail. Interested friends can Refer to the following
Use of this in JavaScript
This variable is an elusive keyword in JavaScript. This can be said to be very powerful. Fully understanding the relevant knowledge of this will help We can write object-oriented JavaScript programs with ease.
The most important thing about this variable is to be able to clarify which object this refers to. Maybe a lot of information has its own explanation, but some concepts are a bit complicated. My understanding is: first analyze which object the function in which this is located is called as a method, then the object is the object referenced by this.
Example 1,
The code is as follows:
var obj = {}; obj.x = 100; obj.y = function(){ alert( this.x ); }; obj.y(); //弹出 100
This code is very easy to understand. When obj.y() is executed, the function is used as the object obj The method is called, so this in the function body points to the obj object, so 100 will pop up.
Example 2,
The code is as follows:
var checkThis = function(){ alert( this.x); }; var x = 'this is a property of window'; var obj = {}; obj.x = 100; obj.y = function(){ alert( this.x ); }; obj.y(); //弹出 100 checkThis(); //弹出 'this is a property of window'
Why 'this is a property of window' pops up here may be a bit confusing. There is a rule in the variable scope of JavaScript "Global variables are properties of the window object". When checkThis() is executed, it is equivalent to window.checkThis(). Therefore, at this time, the point of the this keyword in the checkThis function body becomes the window object, and because the window object has another x attribute ('thisis a property of window' ), so 'this is a property of window' will pop up.
The above two examples are relatively easy to understand, because as long as you determine which object the current function is called as a method (which object is called), you can easily determine the current this variable. direction.
this.x and apply(), call()
Through call and apply, you can redefine the execution environment of the function , that is, the point of this, which is useful for some applications It is very commonly used.
Example 3: call()
The code is as follows:
function changeStyle( type , value ){ this.style[ type ] = value; } var one = document.getElementByIdx( 'one' ); changeStyle.call( one , 'fontSize' , '100px' ); changeStyle('fontSize' , '300px'); //出现错误,因为此时changeStyle中this引用的是window对象,而window并无style属性。
Note that there are three parameters in changeStyle.call(), the first parameter is used to specify the The object on which the function will be called. One is specified here, which means that the changeStyle function will be called by one, so this point in the function body is the one object. The second and third parameters correspond to the two formal parameters type and value in the changeStyle function. The most common effect we see is that the font of Dom element one becomes 20px.
Example 4: apply()
The code is as follows:
function changeStyle( type , value ){ this.style[ type ] = value; } var one = document.getElementByIdx( 'one' ); changeStyle.apply( one , ['fontSize' , '100px' ]); changeStyle('fontSize' , '300px'); //出现错误,原因同示例三
The usage of apply is roughly the same as that of call, with only one difference. apply only accepts two Parameters, the first parameter is the same as call, the second parameter must be an array, and the elements in the array correspond to the formal parameters of the function.
Meaningless (weird) use of this
Example 5:
The code is as follows:
var obj = { x : 100, y : function(){ setTimeout( function(){ alert(this.x); } //这里的this指向的是window对象,并不是我们期待的obj,所以会弹出undefined , 2000); } }; obj.y();
How to achieve the expected effect
The code is as follows:
var obj = { x : 100, y : function(){ var that = this; setTimeout( function(){ alert(that.x); } , 2000); } }; obj.y(); //弹出100
This in the event listening function
The code is as follows:
var one = document.getElementByIdx( 'one' ); one.onclick = function(){ alert( this.innerHTML ); //this指向的是one元素,这点十分简单.. };
Note: Global variables in js will be dynamic Added to the instance window of Window as its property.
This is a keyword in js. The value of this will change as the function is used in different situations. But there is always a principle, that is, this refers to the object that calls the function.
1. Pure function call.
The code is as follows:
function test() { this.x = 1; alert(x); } test();
In fact, this here is the global variable. You can clearly understand by looking at the following example. In fact, this is the global object Global.
The code is as follows:
var x = 1; function test() { alert(this.x); } test();//1 var x = 1; function test() { this.x = 0; } test(); alert(x);//0
2. As a method call, then this refers to the superior object.
The code is as follows:
function test() { alert(this.x); } var o = {}; o.x = 1; o.m = test; o.m(); //1
3. Called as constructor. The so-called constructor function is to generate a new object. At this time, this refers to this object.
The code is as follows:
function test() { this.x = 1; } var o = new test(); alert(o.x);//1
4. Apply call
this points to the first parameter in apply.
The code is as follows:
var x = 0; function test() { alert(this.x); } var o = {}; o.x = 1; o.m = test; o.m.apply(); //0 o.m.apply(o);//1
When apply has no parameters, it is represented as a global object. So the value is 0.
The above is the detailed content of A brief introduction to this variable in JS. For more information, please follow other related articles on the PHP Chinese website!