开发者

Javascript callbacks and optional params

If I am creating a function that takes in two mandatory parameters, one is a call back, and a couple optional how can I code it so that when I call it with only the 2 mandatory parameters it works.

Ex:

function save(color, size, weight,开发者_如何转开发 callback) { ... }

Where color and callback are mandatory and size and weight are optional. So if someone wants to call this function with only color and callback...

save('blue', function(...) { ... }) { ... }

 save('blue', 56, function(...) { ... }) { ... }

But this assigns the callback function to size and weight, how can I fix this to do what I want?


A parameter object solves your problem nicely, like so:

function save(params, callback) {
    params.color = params.color || 'default';
    params.size = params.size || 0;
    params.weight = params.weight || 0;

    // ...

    if (callback instanceof Function) { callback(); }
}

Use like this:

save({ color: 'blue', weight: 100 }, function () { /* ... */ });


JavaScript arguments must be aligned to the expected parameters positionally, so leaving out an argument in between others that are passed isn't going to work. One option is to pass optional arguments in a single object.

// callback is required
// additional arguments are passed as props of 'options'
function save(options, callback) { 
    var color = options.color || 'blue';
    var size = options.size || 72;
}

Pass them like this:

save({ color: 'red' }, function() {});


You can check arguments.length to see how many arguments were passed, and check whether typeof weight === 'function' to see whether an argument is a callback or not.

Using this information, you can figure out what was passed in and re-assign the parameters as necessary.


You could check for the arguments' types: http://jsfiddle.net/wknVA/.


To start with you set the callback default to itself or the next closest param until the end(first) of the optional params,

next you check if the optional params are set as functions (the callback) or undefined and set them to there defaults or set them to themselves.

function optional_params(color, size, weight, callback){
    // set callback to itself or the next optional paramater until you have checked all optional paramaters
  callback = callback || weight || size;
    // check if weight is a function(the callback) or undefined and if so set to a default value, otherwise set weight as weight
  weight = typeof weight != 'function' && typeof weight != 'undefined' ? weight : "1.2kg";
    // do the same as above with weight
  size = typeof size != 'function' && typeof size != 'undefined' ? size : "L";
    // invoke callback
  callback(color,size,weight);
}

And usage:

optional_params("Red",function(color,size,weight){
  console.log("color: " + color); // color: Red
  console.log("size: " + size); // size: L //(default)
  console.log("weight: " + weight); // weight: 1.2kg //(default)
});


In addition to SLaks answer, it is not always possible for the caller to assume they can send whatever args they want, documenting the function which args are optional is the best way, for the function call implementor, type of arg check and grouping of optional args to the end of the arg list are others. Making two options that are optional of the same type asks for confusion.


I recommend ArgueJS for that!

function save() {
  arguments = __({color: undefined, size: [undefined], weight: [undefined], callback: Function})

  // access your args by arguments.paramName. Ex.:
  // alert(arguments.color)
...
}

You can also check the type of your arguments by changing undefined by the desired types.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜