This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 19571 - .length should count optional arguments
Summary: .length should count optional arguments
Alias: None
Product: WebAppsWG
Classification: Unclassified
Component: WebIDL (show other bugs)
Version: unspecified
Hardware: All Windows 3.1
: P2 normal
Target Milestone: ---
Assignee: Cameron McCormack
QA Contact: public-webapps-bugzilla
Depends on:
Reported: 2012-10-17 07:51 UTC by Aryeh Gregor
Modified: 2012-12-07 04:57 UTC (History)
5 users (show)

See Also:


Description Aryeh Gregor 2012-10-17 07:51:07 UTC
Currently the spec says that .length on function objects is equal to the minimum possible number of arguments.  In particular, optional arguments are ignored.  This is bad, because it means that if a spec originally defined f(int x) and later adds an optional argument f(int x, optional int y), there's absolutely no way to feature-test whether the extra argument is supported.  Calling f() with two arguments in old browsers will silently ignore the second argument, which is not useful.

If this is to match ES6, why can't we just change ES6?  Is there a reason ES6 ignores optional arguments for the purposes of .length?  This is a new feature, so we should be able to change it.  All I was able to find on the subject is:

The length property of a function should be updated to not include optional parameters.

— Erik Arvidsson 2010/11/05 22:37

But he doesn't give a reason.

zcorpan just mailed public-script-coord asking that this be changed in both ES6 and WebIDL:

See also Mozilla bug:
Comment 1 Erik Arvidsson 2012-10-17 14:49:03 UTC
There has been lot of discussion regarding this and at the moment we are sticking to what is in the ES6 draft.

Function .length is not very useful either way you go. To be able to do feature detection related to the arity of a function a better reflection mechanism is needed. For example, DOMTokenList.prototype.add changed from "DOMString token" to "DOMString... tokens". Would you want length to be 0, 1 or Infinity in that case? With ES6 length becomes 0 because that is the number of required parameters, the least number of parameters needed for the function to "work".
Comment 2 Rick Waldron 2012-10-17 14:53:02 UTC
In support of Erik's comment, this is the latest relevant discussion thread:
Comment 3 Anne 2012-10-17 19:45:30 UTC
FWIW, I would expect the minimum amount of required parameters for DOMTokenList.prototype.add to be 1, not 0.
Comment 4 Erik Arvidsson 2012-10-17 19:57:09 UTC
(In reply to comment #3)
> FWIW, I would expect the minimum amount of required parameters for
> DOMTokenList.prototype.add to be 1, not 0.

If that is the case we should update the IDL to:

add(DOMString token, DOMString... tokens)

There is no way to look at

add(DOMString... tokens)

and see that it needs at least one parameter.
Comment 5 Anne 2012-10-18 04:42:00 UTC
I thought that would be the difference between "optional DOMString..." and "DOMString...". Hmm. I guess no arguments is okay... it's just a no-op then.
Comment 6 Aryeh Gregor 2012-10-20 17:52:37 UTC
Thanks for the link, Rick!  I read through it briefly, and didn't come away with any specific reasons to stick with the current spec other than "it better matches existing built-ins, and we've discussed this detail far more than its importance warrants, so please let's just stick with what we have".  To be honest, though, that doesn't seem like a bad reason to me.  Even leaving aside the rest parameter, .length is of dubious value anyway.  Plus, three out of four browser engines just always set .length to 0 for WebIDL operations, so it's not like it's really going to be useful to anyone anyway.

I still favor changing it based on the arguments I've seen so far, but if the ES people have discussed it a lot and change looks unlikely, I'm not going to try arguing.  It's not very important and we all have better things to do.  So I'd be okay with WONTFIX as a resolution here, and Mozilla can land the change to match the WebIDL spec.

(An alternative would be for WebIDL to deviate from ES, but that seems even less desirable.)
Comment 7 Cameron McCormack 2012-12-07 04:57:46 UTC
Thanks for looking into this, everyone.  Given that ES6 is staying as it is, I think WONTFIX is the right thing to do.