2

From the bootstrap-datepicker library:

    // .....
    return function(){
        var a = [];
        a.push.apply(a, arguments);
        $.extend(a, extras);
        return a;
    };

What's the point in pushing arguments into an array when there are no arguments?

qed
  • 22,298
  • 21
  • 125
  • 196
  • `arguments` exists, even if you don't have them in the function definition! So `function(a){ return arguments[0]; }` is equal to `function(){ return arguments[0]; }` when called with a single argument. – somethinghere Nov 03 '15 at 16:58
  • You don't know that. This function could be called as `pickDate(12, "mars", 32, { options: "none" })` - `arguments` will still contains all the things passed in, whether or not they get a variable reference. – Katana314 Nov 03 '15 at 16:59

2 Answers2

5

How do you know there's no arguments? It depends on how that function is called. In JavaScript the method signature does not limit how the function is used. You're free to call this with zero or more arguments regardless.

The arguments variable contains whatever arguments are passed in. This function apparently uses that in some capacity.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • @qed it's useful if you want to allow people to pass in an infinite number of arguments - for example: `new Array(/* takes infinite arguments, each an entry */);` (I know its not technically infinite) – somethinghere Nov 03 '15 at 17:01
  • @somethinghere Yeah, this is called `varargs` in other languages, where you're allowed a variable number of arguments. JavaScript has no formal notation for declaring these, but others do it as `f(...)` or `f(*args)` to indicate this. – tadman Nov 03 '15 at 17:02
  • @tadman and ES6 introduces the `...` rest parameter: `function(...myArgumentKey){ return myArgumentKey[0]; }`. Cool times to be alive :) – somethinghere Nov 03 '15 at 17:03
  • 1
    @tadman: ES6 introduces "rest parameters" which makes the intend clearer: `function sum(...numbers) { ... }`. Also `numbers` will be a real array. – Felix Kling Nov 03 '15 at 17:03
  • 1
    @somethinghere Good to know! Yeah, ES6 goes a long way towards cleaning up a lot of the junk in JavaScript. Won't be long until that can be a safe default for production code without using a compiler/translator layer. – tadman Nov 03 '15 at 17:04
0

Here is an example

var minionese = function(){
    console.log(arguments)
}

minionese("Banana", "Bellow") // Output is { '0': 'Banana', '1': 'Bellow' }

In javascript, theorotically a function can accept an infinite number of arguments regardless of how many arguments are specified in the function's method signature. The limit is imposed by environment,

Is there a max number of arguments JavaScript functions can accept?

Community
  • 1
  • 1
S V
  • 570
  • 8
  • 21