在异步 javascript 调用中设置变量
setting variable in asynchronous javascript call
我有一个 class C
的对象 x
。属性 x.myval
应通过调用 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...
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;
}
});
}
我有一个 class C
的对象 x
。属性 x.myval
应通过调用 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...
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;
}
});
}