length property value for functions with parameter enhancements

Andreas Rossberg rossberg at google.com
Sat Aug 27 06:12:54 PDT 2011

On 27 August 2011 00:34, Allen Wirfs-Brock <allen at wirfs-brock.com> wrote:
> Something we need to define for ES.next is how to compute the length
> property value for functions whose formal parameter list includes "optional"
> and/or rest parameters.

True, and actually, there are more issues with length & function
proxies. I don't have my notes with me right now, but for example, it
is not clear at all what length Function.prototype.bind should set
when called on a function proxy. 0? 1? Should it try to get the length
property from the proxy and subtract N? What if length is not defined
on the proxy, or not a (natural) number?

This is probably something the proxy proposal has to resolve
eventually, but it's worth keeping in mind for the broader picture.

> So, what is a length determination algorithm that recognizes optional/rest
> arguments and is consistent with the stated intent of length and (as much as
> possible)existing section 15 definitions.  Here is one proposal:
> The length is 0 only if the formal parameter list is empty.
> For example:
>    function foo() {};  //foo.length==1

You meant ==0 here, right?

> If the formal parameter list includes any non-optional, non-rest formal
> parameters, the length is the total number of non-optional/non-rest formal
> parameters.
> For example:
>     function bar(a,b,c) {} //bar.length=3
>     function baz(a,b,c=0) {} //baz.length=2
>     function bam(a,b,c=0,...d) {} //bam.length==2   BTW, is this legal?

Makes sense. (And yes, I don't see why the latter shouldn't be legal.)

> If there are no non-optional or non-rest formal parameters the length is 1.
>     function bar1(a=0) {} //bar1.length=1
>     function baz1(a=0,b=1,c=2) {} //baz1.length=1
>     function bam1(...a) {} //bam1.length==1

I'm not so sure about this, it seems incoherent. Why not 0, especially
for the first two? You mentioned builtins like Array above, but I
would rather count them as the exception to the rule (especially given
that the builtins don't seem entirely consistent wrt length anyway).

FWIW, one could also argue for setting length to +infinity for
functions with only rest parameters. :)


More information about the es-discuss mailing list