Splat operators in JavaScript, equivalent to *args and **kwargs in Python?

I use Python a lot, and I am just quickly learning JavaScript right now (or should I say re-learning). So, I wanted to ask, what is the equivalent of *args and **kwargs in JavaScript?

The closest idiom for *args would be

    function func (a, b /*, *args*/) {
        var star_args = Array.prototype.slice.call (arguments, func.length);
        /* now star_args[0] is the first undeclared argument */

taking advantage of the fact that Function.length is the number of arguments given in the function definition.

You could package this up in a little helper routine like

    function get_star_args (func, args) {
        return Array.prototype.slice.call (args, func.length);

and then do

    function func (a, b /*, *args*/) {
        var star_args = get_star_args (func, arguments);
        /* now star_args[0] is the first undeclared argument */

If you're in the mood for syntactic sugar, write a function which transforms one function into another one which is called with required and optional arguments, and passes the required arguments along, with any additional optional arguments as an array in final position:

    function argsify(fn){
        return function(){
            var args_in   = Array.prototype.slice.call (arguments); //args called with
            var required  = args_in.slice (0,fn.length-1);     //take first n   
            var optional  = args_in.slice (fn.length-1);       //take remaining optional
            var args_out  = required;                          //args to call with
            args_out.push (optional);                          //with optionals as array
            return fn.apply (0, args_out);

Use this as follows:

    // original function
    function myfunc (a, b, star_args) {
         console.log (a, b, star_args[0]); // will display 1, 2, 3

    // argsify it
    var argsified_myfunc = argsify (myfunc);

    // call argsified function
    argsified_myfunc (1, 2, 3);

Then again, you could just skip all this mumbo jumbo if you are willing to ask the caller to pass the optional arguments as an array to start with:

    myfunc (1, 2, [3]);

There is really no analogous solution for **kwargs, since JS has no keyword arguments. Instead, just ask the caller to pass the optional arguments in as an object:

    function myfunc (a, b, starstar_kwargs) {
        console.log (a, b, starstar_kwargs.x);

    myfunc (1, 2, {x:3});

ES6 Update

For completeness, let me add that ES6 solves this problem with the rest parameter feature. See http://ariya.ofilabs.com/2013/03/es6-and-rest-parameter.html.

From: stackoverflow.com/q/17380315

Back to homepage or read more recommendations: