访问JavaScript外部'这个'来自Promise

时间:2018-04-09 16:28:54

标签: javascript promise signalr

我甚至不知道如何说出我的问题,在我完全搞砸之前,这是我的代码:

className.prototype.requestData = function (start, end) {
    client.server.functionName(parameters)
        .done(function (msg) {
            if (msg) {                
                this.process(msg); //this belongs to client, not className as I want
            }
        })
        .fail(function (error) {
            console.log('failed: ' + error);
        });
}

正如您所看到的,我需要调用process来处理返回的数据,并且我不想使用我为其定义的变量,我想使用{{1我猜这是不可能的,有没有更好的目标实现?

由于

4 个答案:

答案 0 :(得分:2)

执行此操作的一种方法是将this存储在变量中并稍后访问它:

className.prototype.requestData = function (start, end) {
    var _this = this;
    client.server.functionName(parameters)
        .done(function (msg) {
            if (msg) {                
                _this.process(msg); //this belongs to className as you want
            }
        })
        .fail(function (error) {
            console.log('failed: ' + error);
        });
}

使用bind

className.prototype.requestData = function (start, end) {
    client.server.functionName(parameters)
        .done(function (msg) {
            if (msg) {                
                this.process(msg); //this belongs to className as you want
            }
        }.bind(this))
        .fail(function (error) {
            console.log('failed: ' + error);
        });
}

否则,请使用箭头功能(在其他答案中提及),但如果您需要同时访问this es,更多详细信息和示例,则第一个是最佳选择:https://medium.freecodecamp.org/learn-es6-the-dope-way-part-ii-arrow-functions-and-the-this-keyword-381ac7a32881

答案 1 :(得分:2)

如果您在ES6中工作,可以使用async / await:

className.prototype.requestData = async function (start, end) {
    try {
        let result = await client.server.functionName(parameters);
        if (result ) {                
            this.process(result);
        }
    } catch (err) {
        console.log('failed: ' + err);
    }
}

如果你想"链"他们,你可以这样做:

try {
    let result = await client.server.functionName(parameters);
    if (result) {                
        this.process(result);
    }
    // You can just call each in succession
    let other_result = await client.server.someOtherAsyncFunc(other_params);
    if(other_result) {
        // Do more stuff
    }
} catch (err) {
    console.log('failed: ' + err);
}

// You can still call more promise-based functions after the try-catch block
let some_result = await client.server.yetAnotherAsyncFunc(more_params);
// Do other stuff with some_result

答案 2 :(得分:1)

假设您处于支持它们的环境中(此时基本上不是IE),您可以使用Arrow Functions。从链接:

  

当箭头函数在全局上下文中执行时,它不会重新定义它自己的;相反,使用封闭词汇上下文的this值,相当于将其视为闭包值。

className.prototype.requestData = function (start, end) {
    client.server.functionName(parameters)
        .done((msg) => {
            if (msg) {                
                this.process(msg); //this belongs to client, not className as I want
            }
        })
        .fail((error) => {
            console.log('failed: ' + error);
        });
}

如果没有箭头功能,您必须将其存储在闭包变量中。否则,这是不可能的。

答案 3 :(得分:0)

{{1}}