Autobinding destructuring

David Bruant bruant.d at gmail.com
Fri Aug 3 14:45:25 PDT 2012


Hi,

I wrote some code today:

     var cos = Math.cos,
         sin = Math.sin,
         PI = Math.PI;

     // later:

     x1 = x + R*cos(t)*cos(angle) - r*sin(t)*sin(angle);

First of all, it made me realize that the usual example of 'with' (using 
with(Math) and an expression like I showed) turn out to be not so useful 
if we had destructuring (turning my 3 top lines into only one)

But it also made me realize that by default, destructuring returns 
unbound methods. It's perfect for the above use case, but may be 
annoying when you wish to extract functions bound to the object they're 
extracted from:

     var o = {a:1, f: function(){return this.a;}};
     var {f} = o;
     f(); // throw

It reminded me of an ECMAScript Regret submitted by Tom [1] about the 
fact that method are extracted unbound by default.
And after a couple of tweets related to 'with' and the canvas API 
[2][3][4] I wonder: would it be worth having another syntactic form 
doing bound method extraction? I don't have an idea of what the syntax 
would look like, but it seems like a valuable idea.

David

[1] https://github.com/DavidBruant/ECMAScript-regrets/issues/13
[2] https://twitter.com/getify/status/231500606425145344
[3] https://twitter.com/rwaldron/status/231501699183960065
[4] https://twitter.com/rwaldron/status/231503439526518784


More information about the es-discuss mailing list