JS & ES6: accessing static fields from a class

In ES6, given the following example:

export default class MyStyle extends Stylesheet {
   static Color = {
      mainDark: '#000'
   }
   static Comp = {
      ...
      color: Color.mainDark
   }
}

How can I access Color.mainDark (static field)?

+4
source share
2 answers

I assume that you are referring to ES.next (possibly using stage babel-0), as klaemo said you can access it, as you would expect, however, if I remember that there were some problems when using Babel and export the class immediately, so export after class definition if you have any problems:

class MyStyle extends Stylesheet {
   static Color = {
      mainDark: '#000'
   }

  someMethod() {
    console.log(MyStyle.Color.mainDark);
  }
}

export default MyStyle;

You can learn more about the problem of Babylon in Marian's answer on a similar question , which was allegedly recorded in Babel 6.2.1.

+3
'use strict';

 class User {
   constructor(firstName, lastName) {
   this.firstName = firstName;
   this.lastName = lastName;
 }

 static createGuest() {
    return new User("guest", "site");
   }
 };

 let user = User.createGuest();

  alert( user.firstName ); // guest

  alert( User.createGuest ); // createGuest ... (function)

const:

'use strict';

class Menu {
 static get elemClass() {
   return "menu"
 }
}

alert( Menu.elemClass ); // menu

--- this

ES6 -

0

Source: https://habr.com/ru/post/1619465/


All Articles