This pointer for JavaScript

Source: Internet
Author: User

This pointer for JavaScript

This in javascript has rich meanings. It can be a global object, the current object or any object, depending on the function call method. A function can be called in the following ways: as an object method call, as a function call, as a constructor call, apply, or call.

Object method call

When called as an object method, this will be bound to this object.

 
 
  1. var point = { 
  2. x : 0, 
  3. y : 0, 
  4. moveTo : function(x, y) { 
  5.      this.x = this.x + x; 
  6.      this.y = this.y + y; 
  7.      } 
  8. }; 

Point. moveTo (1, 1) // this is bound to the current object, that is, the point object.

Here I want to emphasize that this is used to obtain the corresponding value during function execution, rather than during function definition. Even if an object method is called, if the function attribute of this method is passed into another scope in the form of a function name, the point of this will be changed. Here is an example:

 
 
  1. Var a = {
  2. Aa: 0,
  3. Bb: 0,
  4. Fun: function (x, y ){
  5. This. aa = this. aa + x;
  6. This. bb = this. bb + y;
  7. }
  8. };
  9. Var aa = 1;
  10. Var B = {
  11. Aa: 0,
  12. Bb: 0,
  13. Fun: function () {return this. aa ;}
  14. }
  15. A. fun (3, 2 );
  16. Document. write (a. aa); // 3, this points to the object itself
  17. Document. write (B. fun (); // 0, this points to the object itself
  18. (Function (aa) {// note that the passed function is not the result of function execution.
  19. Var c = aa ();
  20. Document. write (c); // 1. Because fun is executed here, this does not point to the object itself, but to the window
  21. }) (B. fun );

In this case, you will understand. This is a confusing place.

Function call

The function can also be called directly. At this time, this is bound to a global object.

 
 
  1. var x = 1; 
  2.  function test(){ 
  3.  this.x = 0; 
  4.  } 
  5.  test(); 
  6.  alert(x); //0 

But there will be some problems, that is, the function defined inside the function, its this will also point to the global, and it is exactly the opposite of what we want. The Code is as follows:

 
 
  1. Var point = {
  2. X: 0,
  3. Y: 0,
  4. MoveTo: function (x, y ){
  5. // Internal functions
  6. Var moveX = function (x ){
  7. This. x = x; // this is bound to the global
  8. };
  9. // Internal functions
  10. Var moveY = function (y ){
  11. This. y = y; // this is bound to the global
  12. };
  13.  
  14. MoveX (x );
  15. MoveY (y );
  16. }
  17. };
  18. Point. moveTo (1, 1 );
  19. Point. x; // => 0
  20. Point. y; // => 0
  21. X; // => 1
  22. Y; // => 1

We will find that not only the movement we want is not completed, but there will be two more global variables. How can this problem be solved? You only need to save this to a variable when entering the function, and then use this variable. The Code is as follows:

 
 
  1. Var point = {
  2. X: 0,
  3. Y: 0,
  4. MoveTo: function (x, y ){
  5. Var that = this;
  6. // Internal functions
  7. Var moveX = function (x ){
  8. That. x = x;
  9. };
  10. // Internal functions
  11. Var moveY = function (y ){
  12. That. y = y;
  13. }
  14. MoveX (x );
  15. MoveY (y );
  16. }
  17. };
  18. Point. moveTo (1, 1 );
  19. Point. x; // => 1
  20. Point. y; // => 1

Constructor call

You can use this to point to the newly created object when creating a constructor in javascript. In this way, this in the function can be avoided pointing to the global.

 
 
  1. var x = 2; 
  2. function test(){ 
  3. this.x = 1; 
  4. var o = new test(); 
  5. alert(x); //2 

Apply or call

The two methods can switch the Context Environment of the function execution, that is, change the object bound to this. The difference between apply and call is that an array is required when a parameter is input, and an application is passed in separately. So we use apply as an example:

 
 
  1. <pre name="code" class="html">var name = "window"; 
  2. var someone = { 
  3.     name: "Bob", 
  4.     showName: function(){ 
  5.         alert(this.name); 
  6.     } 
  7. }; 
  8.  
  9. var other = { 
  10.     name: "Tom" 
  11. };   
  12.  
  13. someone.showName();  //Bob 
  14. someone.showName.apply();    //window 
  15. someone.showName.apply(other);    //Tom 

As you can see, this points to the object when the method in the normal access object is used. When the apply parameter is used and no parameter is set, the current object of this is global. When the apply parameter is set, the current object of this is this parameter.

Arrow function call

Here, we need to add that this of the arrow function in the Next Generation javascript standard ES6 always points to this when the function is defined, rather than when it is executed. Let's take an example to understand:

 
 
  1. var o = { 
  2.     x : 1, 
  3.     func : function() { console.log(this.x) }, 
  4.     test : function() { 
  5.         setTimeout(function() { 
  6.             this.func(); 
  7.         }, 100); 
  8.     } 
  9. }; 
  10.  
  11. o.test(); // TypeError : this.func is not a function 

The above code will cause an error because the point of this is changed from o to global. We need to modify the above Code as follows:

 
 
  1. var o = { 
  2.     x : 1, 
  3.     func : function() { console.log(this.x) }, 
  4.     test : function() { 
  5.         var _this = this; 
  6.         setTimeout(function() { 
  7.             _this.func(); 
  8.         }, 100); 
  9.     } 
  10. }; 
  11.  
  12. o.test(); 

You can save this by using an external file. The arrow function can be used here. As we have just said, this of the arrow function always points to this when the function is defined, rather than when it is executed. So we can modify the above Code as follows:

 
 
  1. var o = { 
  2.     x : 1, 
  3.     func : function() { console.log(this.x) }, 
  4.     test : function() { 
  5.         setTimeout(() => { this.func() }, 100); 
  6.     } 
  7. }; 
  8.  
  9. o.test(); 

This point points to o. We also need to note that this will not change to the object. We know that call and apply can change the point of this, however, it is invalid in the arrow function.

 
 
  1. Var x = 1,
  2. O = {
  3. X: 10,
  4. Test: () => this. x
  5. };
  6.  
  7. O. test (); // 1
  8. O. test. call (o); // still 1

In this way, you can understand the differences between this bound objects in various cases.


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.