Google Data API上的OOP Javascript回调方法
OOP Javascript callback method on Google Data API
我正试图让我的日历事件阅读器使用javascript OOP。在checkAuth()
函数上,当调用handleAuthResult
时,可以观察到以下内容。
-
当使用
this.handleAuthResult
时,方法handleAuthResult()
中的变量this.config.myvar
变为未定义。 -
当使用
this.handleAuthResult()
时,方法handleAuthResult()
中的变量authResult
变为未定义。
代码:
var config = new Config();
var Manager = new Manager(config);
Manager.checkAuth();
function Config(){
this.myvar = "this is what i want";
this.clientId="client id";
this.scopes="scopes";
}
function Manager(theConfig){
this.config = theConfig;
}
Manager.prototype = {
constructor: Manager,
handleAuthResult : function (authResult) {
console.log(authResult);
console.log(this.config.myvar);
},
checkAuth : function () {
console.log("checkAuth()");
gapi.auth.authorize({client_id: this.config.clientId, scope: this.config.scopes, immediate: true}, this.handleAuthResult);
}
}
在回拨handleAuthResult()
时,我需要同时使用this.config.myvar
和authResult
。
根据@zerkms的评论,我能够用bind
方法解决问题。
我可以使用bind
方法传递当前实例,该方法将在handleAuthResult()
中称为this
作为Manager
。
checkAuth : function () {
console.log("checkAuth()");
gapi.auth.authorize({client_id: this.config.clientId, scope: this.config.scopes, immediate: true}, this.handleAuthResult.bind(this));
}
}
相关文章:
- JavaScript回调函数
- 如何在javascript回调函数中返回多个变量
- Google Data API上的OOP Javascript回调方法
- JavaScript回调函数和Google Feed API
- 行上的Primefaces数据表选择javascript回调
- 使用jasmine测试JavaScript回调函数
- 当页面中的某些脚本已经执行时,javascript回调
- 性能惩罚JavaScript回调函数
- JavaScript回调问题
- 需要Javascript回调ajax解释
- 未通过单击按钮调用JavaScript回调
- Google+登录JavaScript回调问题
- 在对象内完成ajax调用后触发Javascript回调
- 独立线程上的Javascript回调函数
- 循环函数(Javascript回调帮助)
- 使用JavaScript回调函数了解变量作用域和闭包
- 带有参数的Javascript回调函数
- 带有数据库的Javascript回调
- Node.js/express,javascript回调函数未执行
- javascript回调如何知道它们的参数