【问题标题】:TypeScript: Passing parameters into a callback functionTypeScript:将参数传递给回调函数
【发布时间】:2020-08-20 06:49:24
【问题描述】:

以下代码来自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;

【问题讨论】:

    标签: typescript arguments vscode-extensions


    【解决方案1】:

    showSubscriptions 返回另一个函数,它是实际的回调

    function showSubscriptions(account) {
        return function callback() {
            const sub = account.getSubscription()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-28
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多