How come I can call a function with an argument, when the function is defined with no parameters?
javascript
Solution
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".
Problem
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,