调用一个变量,该变量是一个函数,而不向其传递参数

Calling a variable which is a function without passing parameters to it

本文关键字:变量 一个 参数 调用 函数      更新时间:2023-09-26

如果你 https://github.com/hapijs/hapi-auth-basic/blob/master/lib/index.js#L14 看这一行,你可以看到它调用internals.implementation而不传入任何参数,但该方法 https://github.com/hapijs/hapi-auth-basic/blob/master/lib/index.js#L14 有 2 个参数。

如果没有参数传入该方法

,该方法internals.implementation如何工作?

在第 14 行,实际上并没有调用 internals.implementation。相反,对该函数的引用被传递给plugins.auth.scheme(),大概稍后由auth插件调用(实际参数将被传递)。

例如,下面是一个简化版本:

function sampleImplementation(message) {
    alert(message);
}
function useImplementation(implementation, message) {
    implementation.apply(this, [message]); // invoke the function with args
}
useImplementation(sampleImplementation, "hey there!"); // should alert "hey there!"