Automatically binding extracted methods

# Axel Rauschmayer (12 years ago)

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

# Alex Russell (12 years ago)

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

# André Bargull (12 years ago)

@@create allows you to set up instance prototypes, here's an amended version of your example code using ES6 classes: 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] anba/es6draft

# Andrea Giammarchi (12 years ago)

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: webreflection.blogspot.com/2012/11/my-name-is-bound-method-bound.html

summarized as

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:

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:

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.

# Brendan Eich (12 years ago)

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?

# Brian Di Palma (12 years ago)

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.

# Rick Waldron (12 years ago)

On Fri, Jun 14, 2013 at 4:26 PM, Brian Di Palma <offler at gmail.com> 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.

# Brendan Eich (12 years ago)

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

strawman:bind_operator