Function length

Andreas Rossberg rossberg at google.com
Mon Jun 11 05:33:25 PDT 2012


On 10 June 2012 03:52, Irakli Gozalishvili <rfobic at gmail.com> wrote:
> I just noticed strange behavior in spider monkey implementation of rest
> arguments:
>
> (function(a, b, ...rest) {}).length // => 2
>
> I think ignoring `rest` in length is pretty counter intuitive. For example I
> have small utility function that
> I use to dispatch depending on argument length.
>
> var sum = dispatcher([
>   function() { return 0 },
>   function(x) { return x },
>   function(x, y) { return x + y },
>   function(x, y, z) { return Array.prototype.slice.call(arguments,
> 1).reduce(sum, x) }
> ])

I don't think any library should ever rely on f.length. It is not a
reliable source of information (f might use 'arguments' even when the
length is officially 0), and I don't honestly see it being useful for
anything but tertiary debugging purposes.

/Andreas


More information about the es-discuss mailing list