猿问

js回调函数中的this什么情况下会指向调用的那个对象?

代码是这样的:

    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的指向问题是在哪决定的?


慕丝7291255
浏览 666回答 1
1回答
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答