this.someFunction.call(this,param)的目的是什么?

Jef*_*nez 7 javascript backbone.js

我在很多地方遇到过一些具有这种模式的代码:

this.someFunction.call(this, param);
Run Code Online (Sandbox Code Playgroud)

但在我看来,这只是一种更冗长的打字方式

this.someFunction(param)
Run Code Online (Sandbox Code Playgroud)

该模式有时出现在作为回调提供的函数内.碰巧使用Backbone,如果相关的话.像这样的东西:

Backbone.View.extend({
    // other stuff ...

    someFunction: function(param) {
        // ...
    },
    anotherFunction: function() {
        this.collection.on("some_event", function() {
            this.someFunction.call(this, param);
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

该模式是否实际上具有的效果不等于this.someFunction(param)或仅仅是因为关闭没有捕获正确的人而感到紧张this

感谢您的任何见解!

Ber*_*rgi 2

该模式实际上是否具有与 不等价的效果this.someFunction(param)

不,它们确实是一样的。假设这是一个继承自的this.someFunction函数(但这是挑剔的)。.callFunction.prototype

看起来有人过于谨慎,或者代码是没有使用this两次的东西的残留物。或者也许作者意识到了this-context-in-callbacks 问题,但未能正确处理它。