4

Good day!

I stumbled upon something I've never seen in the realm of JavaScript, but I guess it's very easy to explain for someone who knows the language better. Below I have the following function: (Code taken from the Book: "Secrets of the JavaScript Ninja")

function log() {
  try {
    console.log.apply(console, arguments);
  }
  catch(e) {
    try {
      opera.postError.apply(opera, arguments);
    }
    catch(e) {
      alert(Array.prototype.join.call(arguments, " "));
    }
  }
}

As you can see, the function is defined with an empty parameter list, but I was completely puzzled when I saw, later in the book, that they actually use said function like this...

var x = 213;
log(x); //Hmmm, I thought this function had an empty parameter list.

Could someone please explain to me, why is that function call allowed/possible? What are the concepts involved in JS that support this functionality? Thanks in advance, I'm very confused.

Best Regards,

jlstr
  • 2,986
  • 6
  • 43
  • 60
  • 1
    possible duplicate of [Javascript - is it bad to pass more arguments than the function declares?](http://stackoverflow.com/questions/2525568/javascript-is-it-bad-to-pass-more-arguments-than-the-function-declares) or [What happens if I call a JS method with more parameters than it is defined to accept?](http://stackoverflow.com/q/12694031/1048572) – Bergi Mar 10 '13 at 15:34
  • Yes, possible unintentional duplicate. But I never could have guessed I could search a similar question with that title. I'm sorry. – jlstr Mar 10 '13 at 15:44
  • Often times, an external search engine gives much better results than SO's internal search - I found these questions via Google and the "Linked" list on the right. – Bergi Mar 10 '13 at 15:47

4 Answers4

10

You can call functions with the wrong number of parameters as much as you like. Excess parameters will be ignored; missing parameters will be given a default value.

As you can see from the code sample, you can access the arguments that were actually passed with the "arguments" object.

One of the design principles of JavaScript is to be forgiving rather than strict. If there's a way to keep going, JavaScript keeps going. Failing to pass a sufficient number of arguments is not considered fatal in JavaScript the way it would be in a language like C#, where one of the design principles is "bring possible errors to the developer's attention by failing at compile time".

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • Very interesting indeed. Good thing I can always ask here, when this type of things come up, otherwise I'd completely helpless. Incidentally, Does CoffeeScript support this in the same way? – jlstr Mar 10 '13 at 15:51
  • @JoseE: I've never used CoffeeScript so I wouldn't know. – Eric Lippert Mar 10 '13 at 16:00
2

Javascript functions have an implicit arguments parameter which is an array-like object with a length property.

for your log method you could do.

function log(){

  alert(arguments[0]); 

  alert(arguments[1]);

}

And you can call log.

log("first","second");
scartag
  • 17,548
  • 3
  • 48
  • 52
  • `arguments` is not a real array, it's an array-like *object* that has a `length` property. Reference: http://es5.github.com/#x10.6 – DCoder Mar 10 '13 at 15:33
  • Thanks for this discussion, and for the code sample, it's good to that the arguments pseudo-array can be accessed like that. Thanks you Sir! – jlstr Mar 10 '13 at 15:53
1

JavaScript functions are variadic, i.e. they can take an arbitrary and infinite amount of arguments. You can access them via the arguments object, and pass in a variable number of values via apply. The formal parameter list just declares some variable names that are pointers to the arguments, and if there are less arguments given than parameter names defined they will default to undefined.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
1

JavaScript allows you to call functions with any number of parameters, but if you'd like something more rigorous, there is TypeScript.

It prevents incorrect method calls and a lot of other JavaScript silliness.

MgSam
  • 12,139
  • 19
  • 64
  • 95