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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Li Xiaolong

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.


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

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

Bergi
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
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

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

Nicu Micleusanu
In reply to this post by Li Xiaolong
It does not make sense for me because you already have access to all the
values available in the current context, there is no need to make things
more complex and to add a getter.
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss