javascript - ES6 equivalent of the following pattern? -
i want use static class properties (stage-0) in es6 classes -
class button { static size = { small: "small", big: "big" } } class uilibrary { consturctor() { this.button = new button(); } } // can't access static properties of class instance :( const librarya = new uilibrary(); console.log(librarya.button.size.small);
what best alternative this?
edit:
this question not creating class properties in es6/7 supported in stage-0, nor creating static methods. looking find pattern allows attaching of enum-like objects class instances. hence none of duplicate question suggestions valid.
i can't access static properties of class instance :(
yes, if static properties need access them on constructor:
console.log(button.size.small); console.log(librarya.button.constructor.size.small);
(see here discussion of differences)
i looking find pattern allows attaching of enum-like objects class instances.
if want them available on instances, don't make them static
:
class button { // without experimental syntax, assignment in constructor size = { small: "small", big: "big" } }
or, instead of dropping static
keyword, put them on prototype object isn't recreated on , over:
class button {} button.prototype.size = { small: "small", big: "big" };
or maybe shouldn't put them enum on class @ all, , use named exports of es6 module.
Comments
Post a Comment