this depth face questions 3

=. 1 window.val ;
  var obj = { 
   Val: 2 , 
   DBL: function () {
      the this .val * = 2 ; 
     Val * = 2 ; 
     the console.log (Val); 
     the console.log ( the this .val); 
   } 
 } ; 
 // tell output below 
obj.dbl ();
  var FUNC = obj.dbl; 
 FUNC (); 
result:   2488

val variable object prefix is ​​not specified, the default look for from a function, can not be found in looking from the window of global variables

That val * = 2 is window.val * = 2

this.val default refers obj.val; because dbl () was first called directly obj

<2> line 14 calls

func () without any prefix, similar to a global function, namely window.func call, so

When the second call, this refers to the window, val refers window.val

The second result of the first affected

 

var obj = {
say: function () {
return function() {
console.log(this)
}
}
}
obj.say()(); // window

 

var obj = {
say: function () {
return ()=> {
console.log(this)
}
}
}
obj.say()(); // obj

Since there is no default anonymous function of the host object, this refers to the default window function improving arrow

<Script>
  var obj = { 
   say: function () { 
     the setTimeout (() => { 
       the console.log ( this ) 
     }); 
   } 
 } 
 obj.say (); // obj 
</ Script> 
In this case this is inherited from the obj, it refers to the definition of its object obj, rather than the window!

The timer function, since there is no default host object, this refers to the default window

Use the arrow but can improve function

Guess you like

Origin www.cnblogs.com/-constructor/p/11647023.html
Recommended