On JavaScript to get accustomed case in point of this

Summary of JavaScript to get accustomed case in this point, but look at how to modify ⾏ into the record the next point to this
       If the object is in ⾥ ⾯ Using this, shoots as usual terms, this means that the keyword which it objects. this can use it in the Remedies, get access to the object properties.

this points to get accustomed case:

 

          1, the default point: this is the default points to the window object.

// default point 
    console.log ( the this );

 

          2, the function call:

                Independent ① When you call this: pointing to the window object

//   independent call time (who calls the function, this points to whom) 
 function Test () { 

         the console.log ( the this ); 
 } 

Test ();

               ② If the function as a method of an object: this points to the current object.

// a function as an object method invocation, this refers to the current object is 

 the let obj = { 
      name: "Test" , 
      Show: function () { 

          the console.log ( the this ); 

          the console.log ( the this .name); 
      } 
 } 

 obj .Show (); // print: the Test 

// Note: The following is the code to call is: this refers to the window object
      let a = obj.show;

      a();
 
 

 

          3, point to the current object

 

//指向当前对象

 let obj = {
    name:'test',
    age:19,
    show:function(){
         console.log(this);
         console.log(this.name,this.age)
      
         this.sayhello();
    },sayhello:function(){
console.log(
"hello");
}
} obj.show();//打印:test 19 obj.sayhello();//打印:hello

 

          4, this points to binding element: when the function as an event handler, then this is the binding element points

                Of this point into ⾏ Review:. 1, Call ()

                      2、apply()

                      3、bind()

 

// change this point, the call may be used to provide the functions () and Apply (), there es5 in the bind () 
    the let obj = { 
        name: "Hello" , 
            Age: . 19 , 
            Show: function (X, Y) { 
                the console.log ( the this ); 
                the console.log ( the this .name); 
                the console.log ( the this .age + X + Y); 
        } 
    } 

    the let obj2 = { 
        name: "Joe Smith" , 
        Age: 30 
    } 
     obj.show ( 10 , 20); // Print: hello 49
Call // ()
obj.show.call (obj2,10,10); // Print: seating 50
// Apply ()
obj.show.apply (obj2, [10,20]); // Print: Double three 60
// ES5 in the bind () the let = obj.show.bind Test (obj2);
Test (10,30); // Print: Zhang 70

 

// pointing arrow this function: it always points to this point enclosing scope 

document.querySelector ( "Button") the onclick = () =>. { 
  The console.log ( this ); 
} 
the let obj = { 
    name: "Test" , 
    Show :() => { 
        the console.log ( this ); // Since i is a function ⼀ arrow, so this points to the outer layer, so this.name will print Test     } 
} 
obj.show (); // print: the Test

 

 

 
 
 
 
 this points to the current object

 

Guess you like

Origin www.cnblogs.com/suger-4/p/12567714.html