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

Li Xiaolong 898310778 at outlook.com
Tue Sep 27 13:22:50 UTC 2016


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

```

If I want to do this using current syntax, I need to write the following:

```js

var a = 2;

Object.defineProperty(this, 'b', {get: function(){return a+1}});

b;  //returns 3

```

It is much more complex than the syntax I advised above. My advice also
makes defining a getter/setter in current scope look like defining a
getter/setter in an object, which makes it easy to remember.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160927/d59ce27c/attachment-0001.html>


More information about the es-discuss mailing list