Automatically binding extracted methods

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Automatically binding extracted methods

Axel Rauschmayer
Thanks to proxies now having a separate trap for “invoke”, we can automatically bind methods on “get”. I’ve written down my thoughts here:

Axel

-- 
Dr. Axel Rauschmayer



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Automatically binding extracted methods

Alex Russell-4
This, incidentally, is the sort of way I'd hoped we'd slot in soft-binding.


On Fri, Jun 14, 2013 at 11:45 AM, Axel Rauschmayer <[hidden email]> wrote:
Thanks to proxies now having a separate trap for “invoke”, we can automatically bind methods on “get”. I’ve written down my thoughts here:

Axel

-- 
Dr. Axel Rauschmayer



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Automatically binding extracted methods

André Bargull-2
In reply to this post by Axel Rauschmayer
@@create allows you to set up instance prototypes, here's an amended version of your example code using ES6 classes: https://gist.github.com/anba/9f0acbb29bf755d26f37

The updated version also gets rid of the "origProto" workaround, but requires a custom @@hasInstance hook. I've tested the script with [1], not sure if the other ES6 runtimes/transpilers already provide support for @@create and @@hasInstance.


- André

[1] https://github.com/anba/es6draft

Thanks to proxies now having a separate trap for “invoke”, we can automatically bind methods on “get”. I’ve written down my thoughts here:
http://www.2ality.com/2013/06/auto-binding.html

Axel

-- 
Dr. Axel Rauschmayer
axel at rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Automatically binding extracted methods

Andrea Giammarchi-2
In reply to this post by Axel Rauschmayer
I think that's possible since "the beginning of the time" ... we just need to be a bit more pragmatic with what's available.

Examples here:

summarized as

```javascript
Generic.prototype.bound = function (methodName) {
  var boundName = '__secret' + methodName;
  return this[boundName] || (this[boundName] = this[methodName].bind(this));
};
```

What I'd love to see is a Function.prototype.bindOnce though, with a private WeakMap able to do something like:
```javascript
var wm = new WeakMap; Function.prototype.bindOnce = function (context) { return wm.has(context || (context = null)) ? wm.get(context) : wm.set(context, this.bind(context)), wm.get(context); };

// example
obj.method.bindOnce(obj);
```

or, even better, an `Objectprototype.boundTo` method such:

```javascript
Object.defineProperty( Object.prototype, 'boundTo', { value: (function(wm){ return function boundTo(method) { var mirror = wm.get(this) || (wm.set(this, { m: [], // collection of method pointers b: [] // collection of bound methods }), wm.get(this)), i = mirror.m.indexOf(method); if (i < 0) { i = mirror.m.push(method) - 1; mirror.b.push(method.bind(this)); } return mirror.b[i]; }; }(new WeakMap)) } ); // example obj.boundTo(obj.method); obj.boundTo(genericCallback);
```
Done internally would surely perform faster.

Cheers




On Fri, Jun 14, 2013 at 3:45 AM, Axel Rauschmayer <[hidden email]> wrote:
Thanks to proxies now having a separate trap for “invoke”, we can automatically bind methods on “get”. I’ve written down my thoughts here:

Axel

-- 
Dr. Axel Rauschmayer



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Automatically binding extracted methods

Brendan Eich-3
In reply to this post by Alex Russell-4
Alex Russell wrote:
> This, incidentally, is the sort of way I'd hoped we'd slot in
> soft-binding.

I still do not know how to do "soft binding" with acceptable
implementation cost. But anyway, this seems like very hard binding. How
would you rebind |this|, ever?

/be

>
>
> On Fri, Jun 14, 2013 at 11:45 AM, Axel Rauschmayer <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Thanks to proxies now having a separate trap for “invoke”, we can
>     automatically bind methods on “get”. I’ve written down my thoughts
>     here:
>     http://www.2ality.com/2013/06/auto-binding.html
>
>     Axel
>
>     --
>     Dr. Axel Rauschmayer
>     [hidden email] <mailto:[hidden email]>
>
>     home: rauschma.de <http://rauschma.de>
>     twitter: twitter.com/rauschma <http://twitter.com/rauschma>
>     blog: 2ality.com <http://2ality.com>
>
>
>     _______________________________________________
>     es-discuss mailing list
>     [hidden email] <mailto:[hidden email]>
>     https://mail.mozilla.org/listinfo/es-discuss
>
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Automatically binding extracted methods

Brian Di Palma
I suppose a special binding operator has already been discussed and discarded?

const functionBoundToclassInstance = classInstance->methodName

and everytime classInstance->methodName is called it gets returned the
same function
meaning references don't have to be stored for unsubscribing from listeners.


On Fri, Jun 14, 2013 at 7:57 PM, Brendan Eich <[hidden email]> wrote:

> Alex Russell wrote:
>>
>> This, incidentally, is the sort of way I'd hoped we'd slot in
>> soft-binding.
>
>
> I still do not know how to do "soft binding" with acceptable implementation
> cost. But anyway, this seems like very hard binding. How would you rebind
> |this|, ever?
>
> /be
>>
>>
>>
>> On Fri, Jun 14, 2013 at 11:45 AM, Axel Rauschmayer <[hidden email]
>> <mailto:[hidden email]>> wrote:
>>
>>     Thanks to proxies now having a separate trap for “invoke”, we can
>>     automatically bind methods on “get”. I’ve written down my thoughts
>>     here:
>>     http://www.2ality.com/2013/06/auto-binding.html
>>
>>     Axel
>>
>>     --     Dr. Axel Rauschmayer
>>     [hidden email] <mailto:[hidden email]>
>>
>>     home: rauschma.de <http://rauschma.de>
>>     twitter: twitter.com/rauschma <http://twitter.com/rauschma>
>>     blog: 2ality.com <http://2ality.com>
>>
>>
>>     _______________________________________________
>>     es-discuss mailing list
>>     [hidden email] <mailto:[hidden email]>
>>     https://mail.mozilla.org/listinfo/es-discuss
>>
>>
>>
>> _______________________________________________
>> es-discuss mailing list
>> [hidden email]
>> https://mail.mozilla.org/listinfo/es-discuss
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Automatically binding extracted methods

Rick Waldron



On Fri, Jun 14, 2013 at 4:26 PM, Brian Di Palma <[hidden email]> wrote:
I suppose a special binding operator has already been discussed and discarded?

const functionBoundToclassInstance = classInstance->methodName

A thin arrow here will be confusing in the fat-arrow ES6-world-of-tomorrow; additionally, I'd like to any loose reservation a thin-arrow function that does not do lexical |this| binding as fat-arrow does.


Rick


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Automatically binding extracted methods

Brendan Eich-3
To answer Brian's question, yes: already discussed an in strawman space
on the wiki, awaiting promotion to harmony, maybe for ES7:

http://wiki.ecmascript.org/doku.php?id=strawman:bind_operator

/be

Rick Waldron wrote:

>
>
>
> On Fri, Jun 14, 2013 at 4:26 PM, Brian Di Palma <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     I suppose a special binding operator has already been discussed
>     and discarded?
>
>     const functionBoundToclassInstance = classInstance->methodName
>
>
> A thin arrow here will be confusing in the fat-arrow
> ES6-world-of-tomorrow; additionally, I'd like to any loose reservation
> a thin-arrow function that does not do lexical |this| binding as
> fat-arrow does.
>
>
> Rick
>
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss