我有
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) { ... });
}
如何在调用之前检查回调参数的长度?
答案 0 :(得分:10)
使用callback.length
。
任何函数的length
属性都会告诉您the number of named arguments that function expects。