Home >Web Front-end >JS Tutorial >Detailed explanation about this in js
Due to its runtime binding characteristics, the meaning of this in JavaScript is much richer. It can be the global object, the current object or any object, it all depends on the function method of calling. There are several ways to call functions in JavaScript: as an object method, as a function, as a constructor, and using apply or call. Below we will discuss the meaning of this respectively according to the different calling methods.
In JavaScript, a function is also an object, so the function can be used as an attribute of an object. At this time, the function is called a method of the object. When using this kind of call method, this is naturally bound to the object.
##12 |
##var point = { ##x : 0, y : 0, moveTo : function(x, y) { this.x = this.x + x;
this.y = this.y + y;
}
}; point.moveTo(1, 1)//this is bound to the current Object, that is, point object
|
3456
##function makeNoSense(x) {
|
this.x = x;
makeNoSense( 5);
## For internal functions, that is, functions declared in another function body, this method of binding to the global object will cause another problem. We still take the point object mentioned earlier as an example. This time we hope to define two functions in the moveTo method to translate the x and y coordinates respectively. The result may be unexpected. Not only does the point object not move, but there are two more global variables x and y. Listing 4. point.js
|
The above is the detailed content of Detailed explanation about this in js. For more information, please follow other related articles on the PHP Chinese website!