JavaScript overloading with a callback

2019-07-08 08:37发布

Following the pattern recommended in this question, where we have something akin to:

function foo(a, b, opts) {

}

foo(1, 2, {"method":"add"});
foo(3, 4, {"test":"equals", "bar":"tree"});

How would one then include a callback as the final parameter? I have function foo() that should be able to handle both of:

foo(x, y, function() {
  // do stuff
});

And

foo(x, y, z, function() {
  // do stuff
});

Any suggestions?

4条回答
Rolldiameter
2楼-- · 2019-07-08 08:50

So basically you want to accept a variable number of arguments, followed by a callback as the last one? Something similar to how PHP's array_udiff works?

This is fairly simple:

function foo() {
    var args = [], l = arguments.length, i;
    for( i=0; i<l; i++) args[i] = arguments[i];

    var callback = args.pop();
    // now you have your variable number of arguments as an array `args`
    // and your callback that was the last parameter.
}
查看更多
Emotional °昔
3楼-- · 2019-07-08 08:50

Ended up going with the following solution:

foo(args) {}

foo({"x":"add", "callback": function() {
  // do stuff
}});


foo({"x":"equals", "y":"tree", "callback": function() {
  // do stuff
}});

And then simply checking to see if the last arguments value is a function, ala @PaulP.R.O.'s solution above:

if((arguments.length > 1) && (typeof arguments[arguments.length - 1] === 'function')) {
  var callback = arguments[arguments.length - 1];
}
查看更多
欢心
4楼-- · 2019-07-08 08:55

I'd say just use the arguments object:

function foo(a, b) {
    // Do stuff


    // Done stuff


    var alength = arguments.length;

    // If more than 2 arguments and last argument is a function
    if(alength > 2 && typeof arguments[alength - 1] === 'function')
        arguments[alength - 1].call(this);
}
查看更多
混吃等死
5楼-- · 2019-07-08 09:03

You could do similar to .apply. (unlike .call)

function foo(args, cb) {}

foo(['a', 'b', 'c'], function() {} );

And if you need to, you could put them into variables using .shift:

function foo(args, cb) {
    var a = args.shift(),
        b = args.shift();
    // and so on...
}
查看更多
登录 后发表回答