ajax成功时调用self-object失败
call self object fail in ajax success
我定义了这样一个类:
var LoadingPolecy={
initialize:function(){
return function(){
this.initialize.apply(this,arguments);
}
}
}
var AjaxLoadingPolecy= LoadingPolecy.initialize();
AjaxLoadingPolecy.prototype={
initialize:function(name){
this.name=name;
},
AjaxStartPolecy : function(){
...
},
AjaxStopPolecy : function(){
...
},
SetName : function(name){
...
}
}
var TempLoadingPolecy=LoadingPolecy.initialize();
TempLoadingPolecy.prototype={
initialize:function(displayArea,source,data){
this.loadingMsgPolecy = new AjaxLoadingPolecy();
...
},
StartLoadingTempPolecy : function(callback){
this.loadingMsgPolecy.SetName('view');
this.loadingMsgPolecy.AjaxStartPolecy();
var a = $.ajax({
...
success:function(html){
callback(html);
}
});
},
EndLoadingTempPolecy : function(html){
//Cannot call method 'AjaxStopPolecy' of undefined error
this.loadingMsgPolecy.AjaxStopPolecy();
....
}
}
我似乎已经更改了其中的对象,我如何调用/使用我在初始化中定义的变量?
ajax成功回调(大多数其他回调也是如此)不会给您相同的this
。但是,您可以将this
的前一个副本保存到另一个变量中,并以这种方式访问它:
我不明白你问的是代码的哪一部分,但这里有一个简单的例子,你可以从中进行调整:
initialize:function(displayArea,source,data){
StartLoadingTempPolecy : function(callback) {
this.loadingMsgPolecy.SetName('view');
this.loadingMsgPolecy.AjaxStartPolecy();
// save copy of `this` for future use in the success handler
var self = this;
var a = $.ajax({
...
success:function(html) {
// you can use the variable `self` here to access the previous this ptr
callback(html);
}
});
},
相关文章:
- 函数未在Object.keys或Object.getOwnPropertyNames下列出,但可以调用
- 当json解析空响应时,Whatwg-Fetch失败,我该如何防止它
- delete在Object上效率低下,但在DOM Element's的数据属性,与null out相比
- JsFiddle在分叉后描述失败
- 有没有一种方法可以列出Ember.Object的所有绑定
- 如何取消object.prototypes javascript的一个函数
- 为什么是文档.旧版应用程序中的DOM-object.properties为null
- 为什么不'当单元测试出现解析错误时,我的因果报应测试会失败
- 当一些承诺失败时,如何继续使用$q.all()
- 警告:失败的propType:类型为“array”的无效prop应为React的“object”
- 来自 Object 内部的 Jquery Ajax 调用立即失败
- jqxGrid失败并出现错误“;Object[Object Object]没有方法'jqGrid'&”;
- ajax成功时调用self-object失败
- OOOjavascript:object self-reference返回undefined,但控制台显示它存在
- 带有外部依赖项的Jasmine单元测试指令失败,类型错误:'[object对象]'
- 对象.object.constructor.prototype失败
- 在Object. defineproperties中引用self Object
- 通过回调设置object的成员奇怪地失败了
- 通过Mongoose获取集合失败(Object{}没有'find'方法)
- JSON.用object解析数组失败