Bound instance-function vending (was RE: Arrow binding)

Brendan Eich brendan at mozilla.org
Mon Apr 23 11:17:26 PDT 2012


Apologies for sounding schizo. I'm saying prototypal wins at scale and I 
see it more often used, but closure has its fans and it's used too.

With both (plus other approaches such as Angus's) I do not think it's a 
given that in the future, JS will make methods auto-bind |this| via 
prototypal accessors. That is possible today (ES5 and earlier 
getter/setter support in most browsers) and it's quite rare -- I can 
think of only a few cases, one of which is in the new I18N library.

/be

Brendan Eich wrote:
> There's a big penalty even in optimizing VMs in adding (the same) 
> method reference to every instance, vs. sharing it via a prototype. 
> Binding |this| without new syntax, using whatever (closures, .bind), 
> can be optimized but it's a challenge and mainly, users do not  do it.
>
> The functional-combinator OOP style Angus advocates, which relies on 
> .call to delegate, is pretty but it's yet another discipline and not 
> one I see catching on. The language doesn't make it any easier for 
> users than for optimizing VM implementors.
>
> The pattern you show below, the "closure pattern", is advocated (e.g. 
> by crock's book) and it is used. The cost doesn't matter at low object 
> populations and it's pretty enough, besides being already there, that 
> I agree it will be hard to beat in my view.
>
> /be
>
> Domenic Denicola wrote:
>>> Having done this dance a couple of times, let me suggest to you that 
>>> the method form will *eventually* end up at a per-class getter on 
>>> the prototype which vends an instance function which is bound. 
>>> People will (reasonably) want binding of some sort.
>>
>> This sounds lovely. I vaguely remember seeing it on es-discuss in the 
>> past (for one of the built-ins or proposed built-ins, maybe?). But 
>> never in the wild.
>>
>> I'm curious, though---what advantages does this have over simply 
>> setting the method as an instance property? I.e. how does it differ 
>> from the following pattern?
>>
>> function C() {
>>      var that = this;
>>      that.method = function () {
>>          console.log(that);
>>      };
>> }
>> _______________________________________________
>> es-discuss mailing list
>> 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
>


More information about the es-discuss mailing list