如何在调用函数之前检查回调函数的参数个数

Mar*_*cel 4 javascript

我有

function doSomething(callback) {
    if (callback.arguments.length == 1) { // Need help here
        // Some logic here
        callback(obj1);
    }
    else {
        // Some other logic here
        callback(obj1, obj2);
    }
}

if (someLogic) {
    doSomething(function(arg1) { ... });
}
else {
    doSomething(function(arg1, arg2) { ... });
}
Run Code Online (Sandbox Code Playgroud)

在调用之前如何检查回调参数的长度?

DCo*_*der 11

使用callback.length.

length任何函数的属性都会告诉您函数所需的命名参数的数量.