Syntax Proposal: Add getter/setter directly on current scope ??

Bergi a.d.bergi at web.de
Tue Sep 27 14:32:43 UTC 2016


Li Xiaolong schrieb:
> Sometimes, we want to get a representation of another variable, or get an
> abstract representation of some other variables. In this case, we often use
> getter/setter. But defining a getter/setter in current scope is complex.
>
> Can we make adding a getter or setter to current scope easier like the
> following example?
>
> ```js
>
> var a = 2;
>
> get b() {return a+1;}
>
> b;  //returns 3

You already can do that:
```js
var a = 2;
with({get b() { return a+1; }) {
     b // 3
}
```

However, magic bindings (that do something else than variable assignment 
and access) are a source of confusion and usually a bad idea.
Why not simply use a function `b()` that you can call? It's clear what 
happens with that.
We hardly need a special syntax for getter/setter variables in lexical 
scopes.

- Bergi


More information about the es-discuss mailing list