Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check the number of arguments of a callback function before calling the function

Tags:

javascript

I have

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) { ... });
}

How can I check the length of the callback's arguments before calling it?

like image 488
Marcel Avatar asked Apr 04 '14 09:04

Marcel


1 Answers

Use callback.length.

The length property on any function tells you the number of named arguments that function expects.

like image 163
DCoder Avatar answered Nov 15 '22 00:11

DCoder