代码是这样的:
var name = 'outer'; var obj = { name: 'obj', foo: function (arg) { this.run = arg; this.run(); }, bar:function (arg) { arg(); } }; function fx() { alert(this.name); }; obj.foo(fx);//fx中的this指向obj,此时alert的结果是obj obj.bar(fx);//这样调用fx中的this却指向当前作用域,alert的结果却是outer
就像
$(selector).on('click',function(){ //这里的this会指向被选中的标签, //而希望通过这个回调函数来操作当前作用域中的一些属性时就要另想办法});
所以,this的指向问题是在哪决定的?
相关分类