Javascript对象Jquery回调
在对象中,我有一个方法,希望从服务器获取一些信息(JSON格式)。我想将这些数据添加到函数所在的对象中(通过使用setter)。 问题是,这不是我的对象,而是jquery回调。我该如何解决这个问题Javascript对象Jquery回调,javascript,jquery,ajax,object,callback,Javascript,Jquery,Ajax,Object,Callback,在对象中,我有一个方法,希望从服务器获取一些信息(JSON格式)。我想将这些数据添加到函数所在的对象中(通过使用setter)。 问题是,这不是我的对象,而是jquery回调。我该如何解决这个问题 function anObject() { $.get(URL, doTheCallback); function setExample(example) { this.example = example; } function doThe
function anObject() {
$.get(URL, doTheCallback);
function setExample(example) {
this.example = example;
}
function doTheCallback(data) {
this.setExample(data.results[0].example);
}
}
您可以使用
bind
:
$.get(URL,doTheCallback.bind(this));
该方法创建一个新函数,该函数在调用时具有
此关键字设置为提供的值,并具有给定的
调用新函数时,将在任何函数前面提供参数
或者在变量中指定范围,如:
function anObject() {
var that = this;
$.get(URL, doTheCallback);
function setExample(example) {
that.example = example;
}
function doTheCallback(data) {
that.setExample(data.results[0].example);
}
}
您可以使用
bind
:
$.get(URL,doTheCallback.bind(this));
该方法创建一个新函数,该函数在调用时具有
此关键字设置为提供的值,并具有给定的
调用新函数时,将在任何函数前面提供参数
或者在变量中指定范围,如:
function anObject() {
var that = this;
$.get(URL, doTheCallback);
function setExample(example) {
that.example = example;
}
function doTheCallback(data) {
that.setExample(data.results[0].example);
}
}
您可以使用
bind
:
$.get(URL,doTheCallback.bind(this));
该方法创建一个新函数,该函数在调用时具有
此关键字设置为提供的值,并具有给定的
调用新函数时,将在任何函数前面提供参数
或者在变量中指定范围,如:
function anObject() {
var that = this;
$.get(URL, doTheCallback);
function setExample(example) {
that.example = example;
}
function doTheCallback(data) {
that.setExample(data.results[0].example);
}
}
您可以使用
bind
:
$.get(URL,doTheCallback.bind(this));
该方法创建一个新函数,该函数在调用时具有
此关键字设置为提供的值,并具有给定的
调用新函数时,将在任何函数前面提供参数
或者在变量中指定范围,如:
function anObject() {
var that = this;
$.get(URL, doTheCallback);
function setExample(example) {
that.example = example;
}
function doTheCallback(data) {
that.setExample(data.results[0].example);
}
}
如果切换到
$.ajax
,则可以使用上下文选项
function anObject() {
$.ajax(URL, {context: this}).done(doTheCallback);
function setExample(example) {
this.example = example;
}
function doTheCallback(data) {
this.setExample(data.results[0].example);
}
}
如果切换到
$.ajax
,则可以使用上下文选项
function anObject() {
$.ajax(URL, {context: this}).done(doTheCallback);
function setExample(example) {
this.example = example;
}
function doTheCallback(data) {
this.setExample(data.results[0].example);
}
}
如果切换到
$.ajax
,则可以使用上下文选项
function anObject() {
$.ajax(URL, {context: this}).done(doTheCallback);
function setExample(example) {
this.example = example;
}
function doTheCallback(data) {
this.setExample(data.results[0].example);
}
}
如果切换到
$.ajax
,则可以使用上下文选项
function anObject() {
$.ajax(URL, {context: this}).done(doTheCallback);
function setExample(example) {
this.example = example;
}
function doTheCallback(data) {
this.setExample(data.results[0].example);
}
}
第二个也适用于原型吗?当我使用prototype:ReferenceError:that not defined第二个*也可以使用prototype吗?当我使用prototype:ReferenceError:that not defined第二个*也可以使用prototype吗?当我使用prototype:ReferenceError:that not defined第二个*也可以使用prototype吗?当我使用未定义的prototype:ReferenceError:时,我得到一个引用错误