I18n - defining format() and compare() as getters to simplify usage

Nebojša Ćirić cira at google.com
Tue Jan 31 16:23:11 PST 2012


31. јануар 2012. 15.50, Norbert Lindenberg <ecmascript at norbertlindenberg.com
> је написао/ла:

> I can imagine doing this for Collator.prototype.compare because
> Array.prototype.sort is such a common use case for it, but why for the
> format() methods?


I heard a couple of reasons for format methods to be bound:

1. Can be passed as functions, thus hiding the object details
2. Makes it symmetrical to compare (in case we follow Allen's advice)
3. No binding gotchas for users


> We don't want to impose the overhead of creating a bound function on each
> call to format() unless there's a good reason...
>

Would caching first one resolve the overhead, like so (not sure about
syntax):

NumberFormat.prototype = {
  get format(date) {
     var that = this;
     if (that.__bound === undefined)
       that.__bound = function(a) { uses that; return a < b };
     return that.__bound;
  }
}

Of course, doing it for one and not the other is somewhat inconsistent. And
> since ES 5 has Function.prototype.bind (implemented in the leading browsers
> except Safari), it's not hard to bind compare() without library support:
> myArray.sort(collator.compare.bind(collator));
>

Yes, we should probably pick one or the other - go jQuery route and force
developers to .bind manually, or any other library where they either bind
for the users or provide two versions of the same method (one bind the
other one not).

It seems that both ways of solving binding issues are present in the real
world, but we should resolve the issue so we can finalize the spec (and
implementations).

Norbert
>
>
> On Jan 31, 2012, at 10:04 , Nebojša Ćirić wrote:
>
> > We (i18n group) mentioned slight problem with Collator.compare() method
> to Allen at the last i18n meeting.
> >
> > The problem is that you can't do:
> >
> > var col = new Intl.Collator(...);
> > array.sort(col.compare);
> >
> > because of the binding loss.
> >
> > Allen proposed something like this as a possible solution (typing from
> memory):
> >
> > Collator.prototype = {
> >   get compare(a, b) {
> >      var that = this;
> >      return function(a, b) { uses that; return a < b };
> >   }
> > }
> >
> > It seems that all major JS libraries*, except jQuery, try to help user
> not to stumble on this problem. Should we include this into spec?
> >
> > I think that if we do it for compare() method, we should then do it for
> format() methods too, since that would allow user to pass functions around
> instead of objects.
> >
> > This should not apply to supportedLocalesOf functions.
> >
> > What do you think?
> >
> > * http://www.alistapart.com/articles/getoutbindingsituations/ (see
> "JavaScript frameworks do it" section)
> >
> > --
> > Nebojša Ćirić
>
>


-- 
Nebojša Ćirić
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120131/ff38e131/attachment.html>


More information about the es-discuss mailing list