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;

解决方法

SmallLibs = $($(SmallLibSrc:%.cxx=lib%.a):%.c=lib%.a) $($(SmallLibSrc:%.cxx=lib%.so):%.c=lib%.so) 返回另一个函数,它是实际的回调

showSubscriptions