dan*_*ski 0 javascript prototypejs
function ObjectProvider() {
this.url = "ajax/inbounds.php"
this.inBounds = function () {
this.removeMarkers();
var url_string = this.url;
$.getJSON(url_string, function (data) {
for (i = 0; i != data.length; ++i) {
this.createObject(data[i]);
}
});
};
this.createObject = function (_data) {};
this.removeMarkers = function () {};
};
Run Code Online (Sandbox Code Playgroud)
所以这条线
this.createObject( data[i] );
Run Code Online (Sandbox Code Playgroud)
有一些问题,但是
this.removeMarkers();
Run Code Online (Sandbox Code Playgroud)
工作正常。
这两个函数都在 ObjectProvider 对象中定义。已尝试添加一个名为 test() 的函数,但不喜欢在 JSON 回调函数中调用任何内容。
这是一个典型的范围界定问题;this你的回调函数里面不再$.getJSON()一样了this。
this要解决该问题,您必须在调用之前保留对的引用$.getJSON(),例如
var self = this;
$.getJSON(url_string, function(data) {
// self.createObject( data[i] );
});
Run Code Online (Sandbox Code Playgroud)
或者,使用以下方法绑定成功回调函数$.proxy:
$.getJSON(url_string, $.proxy(function(data) {
this.createObject(data[i]);
}, this));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5660 次 |
| 最近记录: |