traits are now impossible in ES6 until ES7 since rev32?

Claude Pache claude.pache at gmail.com
Fri Feb 6 04:37:27 PST 2015


> Le 6 févr. 2015 à 05:47, Luke Scott <luke at cywh.com> a écrit :
> 
> I know traits are not something that will make it into ES6. This was the suggested alternative on the mailing list:
> 
> class Thing extends mixin(Base, Trait1, Trait2) {...}
> 
> Unfortuantly since rev32 this is now seems impossible, as a custom implementation of traits would need to this to work:
> 
> class Foo {
>    constructor() {}
> }
> function ctor() {
>    Foo.call(this); // <— illegal?
> }
> ctor.prototype = Object.create(Foo.prototype);
> ctor.prototype.constructor = ctor;
> new ctor();

That pattern is the exact (almost character-for-character) pattern I use for defining a subclass the pre-ES6 way. Thus, (as you noted in a subsequent message,) just use standard ES6 subclassing, (but beware of enumerability issues).

But, more generally, that raises an interesting issue if you intend to write a general library that works both with ES6-classes and pre-ES6-pseudoclasses: If you use the pre-ES6 pattern, it risks to break on ES6-classes; but if you use the ES6-classes pattern, it will (fortunately) probably work with pre-ES6-pseudoclasses, but it won't compile on implementations that don't support ES6-classes. So, you have to write your library the both ways and do some feature-detection in order to decide what code to serve.

—Claude

> 
> The code I’m working with makes extensive use of traits. Simple inheritance just doesn’t work for what I’m trying to do (and avoid duplicating code). I’m using 6to5, and now Trait.call(this) is failing since I was defining my traits as classes. I can fix this by not using classes for my traits, and 6to5 or traceur cannot enforce Foo.call(this) from being illegal since “this instanceof Foo” is true, but it is sure to break on the real thing.
> 
> Hopefully I’m wrong in that Foo.call(this) is illegal, but if it is, this is a devastating change, especially when traits are scheduled for ES7 or later.
> 
> --
> Luke
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss



More information about the es-discuss mailing list