Home  >  Article  >  Web Front-end  >  How is this variable used in javascript?

How is this variable used in javascript?

伊谢尔伦
伊谢尔伦Original
2017-07-18 09:43:461263browse

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 us be able to 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,

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 called as a method of the object obj, so this in the function body points to the obj object. So 100 will pop up.

Example 2,

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 JavaScript variable scope that "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 two examples above are relatively easy to understand, because as long as you determine which object the current function is called as a method (which object it is called by), you can easily determine the point of the current this variable.

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 very common in some applications.
Example 3: call()

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 which object 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()

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. There is only one difference. apply only accepts two parameters. The first parameter is the same as call. The first parameter is the same as that of call. The two parameters must be an array, and the elements in the array correspond to the formal parameters of the function.

This in the event listening function

var one = document.getElementByIdx( 'one' ); 
one.onclick = function(){ 
alert( this.innerHTML ); //this指向的是one元素,这点十分简单.. 
};

Note: Global variables in js will be dynamically added to the instance of Window as its attribute.

The above is the detailed content of How is this variable used in javascript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn