JavaScript variable number of arguments to function

后端 未结 11 659
太阳男子
太阳男子 2020-11-22 02:14

Is there a way to allow \"unlimited\" vars for a function in JavaScript?

Example:

load(var1, var2, var3, var4, var5, etc...)
load(var1)
相关标签:
11条回答
  • 2020-11-22 02:53

    I agree with Ken's answer as being the most dynamic and I like to take it a step further. If it's a function that you call multiple times with different arguments - I use Ken's design but then add default values:

    function load(context) {
    
        var defaults = {
            parameter1: defaultValue1,
            parameter2: defaultValue2,
            ...
        };
    
        var context = extend(defaults, context);
    
        // do stuff
    }
    

    This way, if you have many parameters but don't necessarily need to set them with each call to the function, you can simply specify the non-defaults. For the extend method, you can use jQuery's extend method ($.extend()), craft your own or use the following:

    function extend() {
        for (var i = 1; i < arguments.length; i++)
            for (var key in arguments[i])
                if (arguments[i].hasOwnProperty(key))
                    arguments[0][key] = arguments[i][key];
        return arguments[0];
    }
    

    This will merge the context object with the defaults and fill in any undefined values in your object with the defaults.

    0 讨论(0)
  • 2020-11-22 02:54

    Although I generally agree that the named arguments approach is useful and flexible (unless you care about the order, in which case arguments is easiest), I do have concerns about the cost of the mbeasley approach (using defaults and extends). This is an extreme amount of cost to take for pulling default values. First, the defaults are defined inside the function, so they are repopulated on every call. Second, you can easily read out the named values and set the defaults at the same time using ||. There is no need to create and merge yet another new object to get this information.

    function load(context) {
       var parameter1 = context.parameter1 || defaultValue1,
           parameter2 = context.parameter2 || defaultValue2;
    
       // do stuff
    }
    

    This is roughly the same amount of code (maybe slightly more), but should be a fraction of the runtime cost.

    0 讨论(0)
  • 2020-11-22 02:54

    Use the arguments object when inside the function to have access to all arguments passed in.

    0 讨论(0)
  • 2020-11-22 02:55

    Another option is to pass in your arguments in a context object.

    function load(context)
    {
        // do whatever with context.name, context.address, etc
    }
    

    and use it like this

    load({name:'Ken',address:'secret',unused:true})
    

    This has the advantage that you can add as many named arguments as you want, and the function can use them (or not) as it sees fit.

    0 讨论(0)
  • 2020-11-22 02:57

    As mentioned already, you can use the arguments object to retrieve a variable number of function parameters.

    If you want to call another function with the same arguments, use apply. You can even add or remove arguments by converting arguments to an array. For example, this function inserts some text before logging to console:

    log() {
        let args = Array.prototype.slice.call(arguments);
        args = ['MyObjectName', this.id_].concat(args);
        console.log.apply(console, args);
    }
    
    0 讨论(0)
提交回复
热议问题