This in the JavaScript language
Because of the nature of its run-time bindings, this meaning in JavaScript is much richer, and it can be a global object, the current object, or any object, depending on how the function is called. The invocation of a function in JavaScript has several ways: as an object method call, as a function call, as a constructor call, and with an apply or call invocation. Below we will discuss the meaning of this separately, depending on the invocation method.
<script>varstr = "I am a String object, I declare it here, but I am not independent of existence!" "varobj = {des: "I am an object, I declare here, I do not exist independently." " }; varFun =function() {Console.log ("I am a Function Object!" Who calls me, who I belong to: ", This ); }; Obj.fun=Fun ; Console.log ( This= = = window);//Print TrueConsole.log (window.str = = = str);//Print TrueConsole.log (window.obj = = = obj);//Print TrueConsole.log (Window.fun = = = Fun);//Print TrueFun ();//Print I am a Function Object! Who calls me, who I belong to: WindowObj.fun ();//Print I am a Function Object! Who calls me, who I belong to: objFun.apply (str);//Print I am a Function Object! Who calls me, who I belong to: Str</script>
Called as an object method
In JavaScript, a function is also an object, so a function can be a property of an object, at which point it is called a method of the object, which is naturally bound to the object when this invocation is used.
Listing 2. Point.js
var point = { 0, 0, function(x, y) { this. x + x; This this. Y + y; } }; Point.moveto (1, 1)//Thisis bound to the current object, that is, the point object
As a function call
The function can also be called directly, at which point this is bound to the global object. In the browser, window is the global object. For example, the following example: When a function is called, this is bound to a global object, and the next execution of an assignment statement is tantamount to implicitly declaring a global variable, which is clearly not what the caller wants.
Listing 3. Nonsense.js
function Makenosense (x) { this. x= x; } Makenosense (5); x; // x has become a global variable with a value of 5
For intrinsic functions, which are functions that are declared in the body of another function, this way of binding to a global object creates another problem. We still use the point object mentioned earlier as an example, this time we want to define two functions within the MoveTo method, translating the x, y coordinates, respectively. The result may be unexpected, not only the point object does not move, but more than two global variables x, Y.
Listing 4. Point.js
varPoint ={x:0, y:0, MoveTo:function(x, y) {//intrinsic Functions varMoveX =function(x) { This. x = x;//where is this bound to? }; //intrinsic Functions varMovey =function(y) { This. y = y;//where is this bound to? }; MoveX (x); Movey (y); } }; Point.moveto (1, 1); Point.x; //==>0Point.y;//==>0X//==>1Y//==>1
This is a JavaScript design flaw, the correct design is that the internal function of this should be bound to its outer function corresponding to the object, in order to circumvent this design flaw, the clever JavaScript programmer came up with a variable substitution method, conventional, the variable is generally named that.
Listing 5. Point2.js
varPoint ={x:0, y:0, MoveTo:function(x, y) {varthat = This; //intrinsic Functions varMoveX =function(x) {that.x=x; }; //intrinsic Functions varMovey =function(y) {that.y=y; } moveX (x); Movey (y); } }; Point.moveto (1, 1); Point.x; //==>1Point.y;//==>1
Called as a constructor function
JavaScript supports object-oriented programming, unlike mainstream object-oriented programming languages, where JavaScript does not have the concept of classes (class), but rather uses a prototype-based (prototype) approach to inheritance. Correspondingly, the constructors in JavaScript are also very special, as are normal functions if not called with new. As another rule of law, the constructor starts with an uppercase letter, reminding the caller to invoke it in the correct way. If called correctly, this binds to the newly created object.
Listing 6. Point.js
function Point (x, y) { this. x = x; this. y = y; }
Invoke with apply or call
Let us again reiterate that in JavaScript functions are objects, objects have methods, and apply and call are methods of function objects. These two methods are exceptionally powerful, and they allow the switch function to execute the context (contextual), which is the object of this binding. This method is used in many JavaScript techniques as well as in class libraries. Let's look at a specific example:
Listing 7. Point2.js
function point (x, y) { this . x = X; Y; this . MoveTo = function (x, y) { this . x = X; Y; }} var p1 = new point (0, 0); var P2 = {x:0, Y:0}; P1.moveto ( 1, 1); P1.moveTo.apply (P2, [ ten]);
In the example above, we use the constructor to generate an object P1, which has a moveTo method, and another object is created using the object literal P2, and we see that using apply can apply the P1 method to P2, when this is also bound to the object P2. Another method call has the same function, the difference is that the last parameter is not uniformly passed in as an array, but separately passed in.
A different angle to understand
If like the author, we also think that the above four ways not easy to remember, after a period of time, and do not understand what this refers to. So I recommend this article to Yehuda Katz: Understanding JavaScript Function Invocation and "this". In this article, Yehuda Katz takes the method of apply or call as the basic way of function invocation, and several other ways are evolved on this basis, or called syntactic sugars.
Yehuda Katz emphasizes the process of this binding on a function call, regardless of how the function is called, to complete the binding process, unlike when the this is bound to a global object as a function call, and when called as a method, this binds to the object to which the method belongs .
Original: http://www.ibm.com/developerworks/cn/web/1207_wangqf_jsthis/
The This in JavaScript