如何将回调函数包装在其他回调函数中并从中调用
How to wrap callback function in other callback function and call from there?
以下是我的代码:
var me = this;
gapi.auth.authorize({ client_id: client, scope: scope, immediate: true }, function (authResult: any) {
if (authResult && !authResult.error) {
me.accessToken = authResult.access_token;
} else {
//TODO : show error in front end
}
});
当我使用这样的回调函数时。
gapi.auth.authorize({ client_id: client, scope: scope, immediate: true }, AuthResult);
function AuthResult(authResult: any) {
if (authResult && !authResult.error) {
me.accessToken = authResult.access_token;
} else {
//TODO : show error in front end
}
我没有得到回调函数中的me属性
我如何将回调函数包装在其他回调函数中,在那里我也可以在JS 中获得作用域
使用胖箭头:
gapi.auth.authorize({ client_id: client, scope: scope, immediate: true },AuthResult);
const AuthResult = (authResult: any) => {
if (authResult && !authResult.error) {
this.accessToken = authResult.access_token;
} else {
//TODO : show error in front end
}
更多
不使用.bind
:https://basarat.gitbooks.io/typescript/content/docs/tips/bind.html至少还没有
使用.bind
:
gapi.auth.authorize(..., AuthResult.bind(this));
function AuthResult(...) {
// use `this` instead of `me`
}
另请参阅如何在回调中访问正确的"this"上下文?
相关文章:
- 如何将一个JavaScript函数回调为多个函数
- Meteor:异步函数回调异常:onAfterAction
- 从类方法中的 ajax post 函数回调函数更改 javascript 类属性
- jQuery动画函数回调错误
- jquery在html属性中添加函数回调,以便在其他事件中调用
- jquery getjson 函数:回调返回错误的字符串
- postMessage - 多个 postMessage 事件/函数/回调
- 在 Promise 调用的错误函数回调后附加对象的用法是什么
- 单击事件后的 JavaScript 函数回调
- 设置超时函数回调静态变量
- Jquery UI 模式匿名函数回调以打开对话框
- 函数回调、局部变量和 chrome.storage.sync.get
- Jquery Ajax 没有拾取选项参数中指定的函数回调
- 如何在 javascript 中进行函数回调
- 用函数回调封装JavaScript
- jQuery JavaScript嵌套异步函数回调
- Node.js-匿名函数回调
- 函数回调超出范围
- php代码的Javascript函数回调
- 如何使用函数回调在更改时提交表单