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);
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
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).
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.
I don't think it will mess anything up unless you are explicitly dealing with the implicit arguments array. Why are you doing this though?
Try taking a look at this post and perhaps this one.
From MDN:
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.