Wrap function with default arguments. - Node.js Data Type

Node.js examples for Data Type:function

Description

Wrap function with default arguments.

Demo Code


/**// w w w . j av a  2 s. c  om
 * Wrap function with default arguments.
 * Usage:
 * var f_orig = function(a, b){ ... };
 * var a = 1;
 * var b = 2;
 * var f_wrapped = Globa.wrapFunc(f_orig, a);
 * 
 * f_wrapped(b); // this execute f_orig and pass a as first argument and b as second 
 * 
 * @param {Function} func function to wrap
 * @return {Function} wrapped function
 */
var wrapFunc = function(func){
    var makeArray = Array.prototype.slice;
    var wrapArguments = makeArray.call(arguments, 1);          
    return function(){ return func.apply(this, wrapArguments.concat(makeArray.call(arguments))); };              
};

Related Tutorials