【问题标题】:setting variable in asynchronous javascript call [duplicate]在异步javascript调用中设置变量[重复]
【发布时间】:2018-08-20 12:48:01
【问题描述】:

我有一个 xC 的对象。属性x.myval 应通过调用x.load() 来设置,而x.load() 又应从异步ajax 调用中获取其数据。

class C {
    constructor() {
        this.myval = "hello";
    }
    load() {
        return $.ajax({
            type: "GET",
            url: "file.txt",
            // suppress "xml not well-formed error in firefox", 
            beforeSend: function(xhr){
                if (xhr.overrideMimeType) { xhr.overrideMimeType("text/plain"); }
            },
            contentType: "text/plain",
            dataType: "text",
            success: function(text) {
                this.myval = text;
            }
        });
    }
}

var x = new C();
$.when(x.load()).done(function(a1,a2) {
    console.log(x.text); //should print the content of file.txt
});

我收到错误this.myval is undefined,显然是因为this 设置为jquery-$this

我也试过了:

class C {
    constructor() {
        this.myval = "hello";
    }
    load() {
        var callback = function callbackClosure(mythis) {return function(text) {
            this.myval = text;
        }}(this);

        return $.ajax({
            type: "GET",
            url: "file.txt",
            // suppress "xml not well-formed error in firefox", 
            beforeSend: function(xhr){
                if (xhr.overrideMimeType) { xhr.overrideMimeType("text/plain"); }
            },
            contentType: "text/plain",
            dataType: "text",
            success: callback
        });
    }
}

var x = new C();
$.when(x.load()).done(function(a1,a2) {
    console.log(x.text); //should print the content of file.txt
});

但这导致了一个例外jQuery.Deferred exception: assignment to undeclared variable...

【问题讨论】:

  • 使用箭头函数。

标签: jquery asynchronous-javascript


【解决方案1】:

this 在这一行中没有引用您的实例:

success: function(text) {
    this.myval = text;
}

您可以在外部方法中创建一个引用您的实例的临时变量,如下所示:

load() {
    var that = this;
    return $.ajax({
        type: "GET",
        url: "file.txt",
        // suppress "xml not well-formed error in firefox", 
        beforeSend: function(xhr){
            if (xhr.overrideMimeType) { xhr.overrideMimeType("text/plain"); }
        },
        contentType: "text/plain",
        dataType: "text",
        success: function(text) {
            that.myval = text;
        }
    });
}

或者,您可以使用箭头函数:

load() {
    return $.ajax({
        type: "GET",
        url: "file.txt",
        // suppress "xml not well-formed error in firefox", 
        beforeSend: function(xhr){
            if (xhr.overrideMimeType) { xhr.overrideMimeType("text/plain"); }
        },
        contentType: "text/plain",
        dataType: "text",
        success: (text) => {
            this.myval = text;
        }
    });
}

【讨论】:

  • 他们没有...有更优雅的解决方案。此外,如果将来有重复(规范),您是否也可以投票关闭而不是立即回答?这是一个非常普遍的问题。
  • 是的,我改写了。箭头函数是要走的路。
  • @Li357 这确实非常普遍。在这个stackoverflow.com/questions/51929737 之前大约一个小时,我已经回答了完全相同的问题。但是很难将此标记为另一个问题的重复。我找不到任何适合的问题。
  • 那就别再回答了。投票以重复结束。
  • @Li357 想想像我这样的菜鸟:我很高兴我得到了答案。在发布这个问题之前,我已经搜索了一个多小时。
猜你喜欢
  • 1970-01-01
  • 2019-10-29
  • 2011-08-22
  • 1970-01-01
  • 2014-06-29
  • 1970-01-01
  • 1970-01-01
  • 2022-11-26
  • 2020-01-21
相关资源
最近更新 更多