tc39 / proposal-bind-operator

This-Binding Syntax for ECMAScript
1.74k stars 30 forks source link

Dynamically named virtual methods? #14

Closed gsklee closed 8 years ago

gsklee commented 9 years ago

Is dynamically named virtual methods somewhere on the road map? Something like this would be really handy and make it parallel with "real" methods in terms of features:

const assign = function(...s) {
  return Object.assign(this, ...s);
};

const name = 'assign';

console.log(
  {x: 1}::[name]({y: 2})
);

// {"x":1,"y":2}
nathggns commented 9 years ago

Variable variables aren't a thing in ES6 at all are they? If not then this couldn't be a simple syntactical transform.

Sent from my iPhone

On 25 May 2015, at 10:06, G. Kay Lee notifications@github.com wrote:

Is dynamically named virtual methods somewhere on the road map? Something like this would be really handy and make it parallel with "real" methods in terms of features:

const assign = function(...s) { return Object.assign(this, ...s); };

const name = 'assign';

console.log( {x:1}::name ); — Reply to this email directly or view it on GitHub.

zenparsing commented 8 years ago

This is basically eval which we want to avoid.

bergus commented 8 years ago

You can always use property lookup for dynamic names - limiting ("whitelisting") the available methods to only that available on the object is a desirable side effect.

const methods = {assign}; // and more
                          // use {__proto__:null, assign} to be safe
let x = {x: 1};
let name = "assign";
x::(methods[name])({y: 2});
console.log(x);
gsklee commented 8 years ago

@bergus Good idea, thanks! :+1: