Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if I call a JS method with more parameters than it is defined to accept?

I'd like to know both for regular all-in-the-family JS developer(me)-defined functions, as well as predefined DOM methods. Like what happens if I try to call IE's attachEvent with the signature of the WHATWG's addEventListener? For instance,

elem.attachEvent('onbillgates\'mom', function(e){ this.mount(); }, false);

Specifically, note the false. Will that trip anything up, even though the attachEvent method's signature only calls for two arguments?

Thanks.

function foo(FirstOf2, SecondOf2) {
  console.log(FirstOf2 + SecondOf2);
}

foo(1, 2, true);
like image 973
wwaawaw Avatar asked Oct 02 '12 16:10

wwaawaw


People also ask

What happens if you pass an extra parameter to a method in JavaScript?

JavaScript is extremely broad-minded about the number of arguments you pass to a function. If you pass too many, the extra ones are ignored. If you pass too few, the missing parameters get assigned the value undefined .

Is it possible to pass more parameters in a function call than expected in a function in TypeScript?

In JavaScript, if you call a function with more arguments than there are parameters, the extra arguments are simply ignored. TypeScript behaves the same way. Functions with fewer parameters (of the same types) can always take the place of functions with more parameters.

What happens if you don't pass any parameter in the JavaScript function?

Nothing will happen- meaning you won't get an error or a warning as passing the parameters in javascript is optional. All the parameters that weren't "supplied" will have the undefined value.

What will happen if you call any function with more or less arguments than required?

If you manage to write code that calls a function with an incorrect number of arguments and get it past the compiler, the behavior is undefined. It might appear to "work", but it could blow up in your face when, for example, you compile it with a different compiler, or with the same compiler and different options.


3 Answers

JavaScript doesn't have the concept of a fixed parameter list. For your own functions you can always specify as many parameters as you want and pass in as many as you want which ever type you want.

For built-in functions, which correlate to native code, it depends.

You asked on what it depends:

Let's look at the ECMA-262

Section 15 about built-in (not to confuse with host) functions in general

Unless otherwise specified in the description of a particular function, if a function or constructor described in this clause is given fewer arguments than the function is specified to require, the function or constructor shall behave exactly as if it had been given sufficient additional arguments, each such argument being the undefined value.

Alright. If I pass in less arguments than needed, it depends on the spec of the function itself (scroll down section 15 to find the spec for each built-in function).

Unless otherwise specified in the description of a particular function, if a function or constructor described in this clause is given more arguments than the function is specified to allow, the extra arguments are evaluated by the call and then ignored by the function. However, an implementation may define implementation specific behaviour relating to such arguments as long as the behaviour is not the throwing of a TypeError exception that is predicated simply on the presence of an extra argument.

Passing in too many arguments should never raise a TypeError. But still it may raise other errors. Again, it depends on the function you talk about.

You were talking explicitly about the DOM and not about built-in functions. To be honest I can't find the corresponding parts of the spec. The ECMA spec is so much easier to read then the w3 website.

like image 108
Prinzhorn Avatar answered Sep 23 '22 07:09

Prinzhorn


Won't hurt. You can even call a function with less parameters than it takes, as long as the function code is ok with a few undefined values.

like image 29
Diogo Schneider Avatar answered Sep 20 '22 07:09

Diogo Schneider


I came across this important, however old, question; and I hope it'll be beneficial for future generations to share my experiments with it:

  1. One can use the arguments object in order to access a function's arguments, regardless of the amount of arguments in the function's signature.
    It's worth mentioning that this doesn't apply to arrow functions:

function singleArg(x) {
  console.log(arguments);
}

singleArg(1, 2); // Called with 2 arguments
singleArg();     // Called with 0 arguments

// Results in an error, as 'arguments' isn't defined for arrow functions
((arg) => console.log(arguments))(1);
  1. It's stated in the documentation that arguments isn't exactly an Array:

“Array-like” means that arguments has a length property and properties indexed from zero, but it doesn't have Array's built-in methods like forEach() and map().

Hence the following code results in an error:

(function singleArg(x) {
  console.log(arguments); // This line works
  arguments.forEach(x => console.log(x)); // This causes an error
})(1, 2);
  1. Upon calling a function with less arguments than in its signature, they're assigned undefined:

(function twoArgs(a, b) {
  console.log(`a: ${a}\nb: ${b}`);
})(1);
like image 43
GalAbra Avatar answered Sep 23 '22 07:09

GalAbra