Class syntax enhancements

/#!/JoePea joe at trusktr.io
Sat Oct 8 23:31:28 UTC 2016


Hello Kagami

This works in your simple text case, but I think it may fall apart in more
complicated cases. f.e.:

```
class A {
  foo() {
    return "foo"
 }
}

class B extends A {
}

partial(B, class extends SomeOtherClass {
  foobar() {
    return `${super.foo()}bar`;
  }
})

output.value = new B().foobar(); // will be “foobar;
```

now the `partial` function will remove the SomeOtherClass prototype chain
and replace it with the chain from B, which would be undesirable (me
thinks).

So far, while considering that [super is static](https://esdiscuss.org/
topic/the-super-keyword-doesnt-work-as-it-should), the best solution to
mixing in other classes is [class-factory-style mixins](
http://justinfagnani.com/2015/12/21/real-mixins-with-javascript-classes/).

But, I think that once Reflect.construct and Proxy is released in Safari
10, it may be possible to write a new multiple-inheritance technique that
works with vanilla classes (without requiring class factories). I'm going
to try it when Proxies are native in all browsers. It's currently not
possible with polyfills, since the behavior can't be polyfilled 100%
accurately.

For reference, here's my first two attempts:

1. https://gist.github.com/trusktr/05b9c763ac70d7086fe3a08c2c4fb4bf
2. https://gist.github.com/trusktr/8c515f7bd7436e09a4baa7a63cd7cc37

Usage would be something like this:

```js
class A extends B {
  // ...
}

class C extends D {
  // ...
}

// extend multiple classes, the one listed first has priority regarding
property lookup:
class E extends multiple(A, C) {
  // ...
}
```

So far, my implementations work for basic classes, but you'll run into
problems in some cases (f.e. differing constructor signatures, and issues
with the super keyword).


*/#!/*JoePea

On Mon, Sep 26, 2016 at 9:10 PM, Kagami Rosylight <saschanaz at outlook.com>
wrote:

> I’m not sure it’s okay to reply on this very old thread, but if I’m
> understanding correctly the desugared code can be fairly short with new
> Object.getOwnPropertyDescriptors:
>
>
>
> ```js
>
> function partial(base, extension) {
>
>   extension.prototype.__proto__ = base.prototype.__proto__; // to enable
> 'super' reference
>
>   const descriptors = Object.getOwnPropertyDescripto
> rs(extension.prototype);
>
>   delete descriptors.constructor; // must not override constructor
>
>   Object.defineProperties(base.prototype, descriptors);
>
>
>
>   return base;
>
> }
>
> ```
>
>
>
> ```
>
> class A {
>
>   foo() {
>
>     return "foo"
>
>  }
>
> }
>
>
>
> class B extends A {
>
> }
>
>
>
> partial(B, class {
>
>   foobar() {
>
>     return `${super.foo()}bar`;
>
>   }
>
> })
>
>
>
> output.value = new B().foobar(); // will be “foobar;
>
> ```
>
>
>
> This sample currently only works on Firefox 50+.
> https://jsfiddle.net/v8bbvavv/
>
>
>
> Sent from Mail <https://go.microsoft.com/fwlink/?LinkId=550986> for
> Windows 10
>
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20161008/db734b70/attachment.html>


More information about the es-discuss mailing list