JavaScript中this指针指向的彻底理解

this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁实际上this的最终指向的是那个调用它的对象

这一点与函数中自由变量Action-varibal不同

 1      var object = {
 2             name : 'The Kite Runner',
 3             obj : {
 4                 name : 'childProperty',
 5                 fn : function(x, y){
 6                     var result = x + y;
 7                     console.log('this-context'+this);
 8                     console.log('this-property'+this.name);
 9                     console.log('result'+result);
10                 }
11             }
12         }
13 
14         var f = object.obj.fn;  //函数引用
15 
16         f();  // this --window
17 
18         //修改this指向--object.obj对象
19         f.apply(object.obj, [10,25]); 
20         f.call(object.obj, 25,45);

码农网 http://www.codeceo.com/article/javascript-this-pointer.html

一个程序员的自我修养 http://kb.cnblogs.com/page/545784/

原文地址:https://www.cnblogs.com/zjf-1992/p/5529866.html