Function length

Rick Waldron waldron.rick at gmail.com
Sat Jun 9 21:08:58 PDT 2012



On Saturday, June 9, 2012 at 11:53 PM, Erik Arvidsson wrote:

> Don't you want to use arguments.length instead of function.length? 
> On Jun 9, 2012 6:53 PM, "Irakli Gozalishvili" <rfobic at gmail.com (mailto:rfobic at gmail.com)> wrote:
> > I just noticed strange behavior in spider monkey implementation of rest arguments: 
> > 
> > (function(a, b, ...rest) {}).length // => 2
I agree that this is strange, I would expect 3, as there are three formally named parameters. 

Rick

 
> > 
> > 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) }
> > ]) 
> > 
> > This behavior of rest would obviously break assumptions made by this library. That being said I don't think `3` would be any better. Maybe such functions length should be: 
> > 
> > - Infinity ?
> > - 2.5 ?
> > 
> > That way libraries would be able to handle them in a nice way: 
> > 
> > var sum = dispatcher([
> >   () => 0,
> >   (x) => x,
> >   (x, y) => x + y,
> >   (x, ...rest) => rest.reduce(sum, x)
> > ])
> > 
> > 
> > Regards
> > --
> > Irakli Gozalishvili
> > Web: http://www.jeditoolkit.com/
> > 
> > 
> > _______________________________________________
> > es-discuss mailing list
> > es-discuss at mozilla.org (mailto:es-discuss at mozilla.org)
> > https://mail.mozilla.org/listinfo/es-discuss
> > 
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
> 
> 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120610/3d500c49/attachment-0001.html>


More information about the es-discuss mailing list