typescript - TypeScript:将参数传递给回调函数
问题描述
以下代码来自azure-account
VSCode 扩展的代码示例的源代码。
export function activate(context: ExtensionContext) {
const azureAccount = extensions.getExtension<AzureAccount>('ms-vscode.azure-account')!.exports;
const subscriptions = context.subscriptions;
subscriptions.push(commands.registerCommand('azure-account-sample.showSubscriptions', showSubscriptions(azureAccount)));
subscriptions.push(commands.registerCommand('azure-account-sample.showAppServices', showAppServices(azureAccount)));
}
如您所见,代码定义了两个命令,这意味着当用户使用命令时azure-account-sample.showSubscription
,它会调用函数showSubscriptions(azureAccount)
。
但是物体怎么会azureAccount
这样通过???在我看来,代码应该这样写:
commands.registerCommand('azure-account-sample.showSubscriptions', showSubscriptions, azureAccount);
//commands.registerCommand
function registerCommand(callback, ...args){
callback(args);
}
//defination of registerCommand from the source code of vscode api
export function registerCommand(command: string, callback: (...args: any[]) => any, thisArg?: any): Disposable;
解决方案
showSubscriptions
returns another function which is the actual callback
function showSubscriptions(account) {
return function callback() {
const sub = account.getSubscription()
}
}
推荐阅读
- c# - html/web 抓取 C# 特定文件结构
- json - REST:创建基于 Json 的查询:使用哪种 http 方法?
- raku - 在数字上下文 raku 中使用 Any 类型的未初始化值
- android - 如何在没有budle的情况下将数据从活动传递到android中的片段?
- entity-framework - EF Core 3 GroupBy 多列计数带扩展的抛出但 linq 有效
- tensorflow - GCP AI Platform Notebook 中 No module named 'tensorflow' 的错误消息
- javascript - 通过 POST 方法将 Javascript 中动态生成的复杂结构的表单提交到 PHP 脚本
- python - AttributeError:“InstagramBot”对象没有属性“登录”
- facebook-graph-api - 当在 facebook 中发布带有特定字符串的帖子时,如何从 facebook 获取 webhook 通知
- reactjs - 尽管创建了文件对象,但 Multer req.file 始终未定义