Native Proxy Syntax

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

Native Proxy Syntax

Vihan Bhargava
The `Proxy` class is great for classes however at the moment, the current syntax can be unwieldy:

```
class MyClass {
   constructor() {
       return new Proxy(this, {
           get: function(target, name) {
               if (name in target) return target[name];
               // ... do something to determine property
           }
       });
   }
}
```

My proposal is to introduce a more idiomatic syntax for proxies in classes:

```
class MyClass {
   constructor () { ... }
   get *(name) {
       // ... do something to determine property
   }
}
```

This already is much more clear than the above.
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Native Proxy Syntax

Matthew Robb
I feel like this would be easier handled as a subclass helper:
```
f
​unction asProxy(traps={}, Class=function asProxy(){}) {
  return class extends Class {
    constructor() {
       super(...arguments);
       return new Proxy(this, traps);
    }
  }
}​

class MyClass extends asProxy({ ... }) { ... }

class MyOtherClass extends asProxy({ ... }, MyClass) { ... }
```


- Matthew Robb

On Wed, Aug 23, 2017 at 11:08 AM, Vihan Bhargava <[hidden email]> wrote:
The `Proxy` class is great for classes however at the moment, the current syntax can be unwieldy:

```
class MyClass {
   constructor() {
       return new Proxy(this, {
           get: function(target, name) {
               if (name in target) return target[name];
               // ... do something to determine property
           }
       });
   }
}
```

My proposal is to introduce a more idiomatic syntax for proxies in classes:

```
class MyClass {
   constructor () { ... }
   get *(name) {
       // ... do something to determine property
   }
}
```

This already is much more clear than the above.
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


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

RE: Native Proxy Syntax

Alex Kodat-2
In reply to this post by Vihan Bhargava
It would seem that a more general solution would be to allow let inside a
class definition and have the constructor close over the variables declared
in the class:

class MyClass {
    let get = (target, name) => {
        if (name in target) return target[name];
        // ... do something to determine property
    }
    constructor() {
        return new Proxy(this, {get});
    }
}
```

While this does require an extra return statement it's reasonably tidy and
has the huge benefit of providing private variables in a class (as local
variables in a closure). But I have to believe that this has been discussed
ad nauseum and there's probably a very good reason let (or some equivalent)
was not part of class.

-----Original Message-----
From: es-discuss [mailto:[hidden email]] On Behalf Of Vihan
Bhargava
Sent: Wednesday, August 23, 2017 10:08 AM
To: [hidden email]
Subject: Native Proxy Syntax

The `Proxy` class is great for classes however at the moment, the current
syntax can be unwieldy:

```
class MyClass {
   constructor() {
       return new Proxy(this, {
           get: function(target, name) {
               if (name in target) return target[name];
               // ... do something to determine property
           }
       });
   }
}
```

My proposal is to introduce a more idiomatic syntax for proxies in classes:

```
class MyClass {
   constructor () { ... }
   get *(name) {
       // ... do something to determine property
   }
}
```

This already is much more clear than the above.
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

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

Re: Native Proxy Syntax

Rick Waldron
In reply to this post by Vihan Bhargava
Inline...

On Wed, Aug 23, 2017 at 11:08 AM Vihan Bhargava <[hidden email]> wrote:
The `Proxy` class is great for classes however at the moment, the current syntax can be unwieldy:

```
class MyClass {
   constructor() {
       return new Proxy(this, {
           get: function(target, name) {
               if (name in target) return target[name];
               // ... do something to determine property
           }
       });
   }
}
```

My proposal is to introduce a more idiomatic syntax for proxies in classes:

```
class MyClass {
   constructor () { ... }
   get *(name) {
       // ... do something to determine property
   }
}
```

This looks too much like GeneratorMethod syntax: 

class Foo {
  constructor() {}
  * gen() {}
}

Rick

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss