13

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 (the static field)?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Livioso
  • 1,242
  • 1
  • 11
  • 21
  • 2
    You cannot. It's not defined until the closing `}` on the last line – zerkms Dec 11 '15 at 09:32
  • 1
    in a method you could do `MyStyle.Color.mainDark`. – klaemo Dec 11 '15 at 09:33
  • 5
    That's not ES6. That's some weird experimental (ES7-proposed) property intialisers. – Bergi Dec 11 '15 at 09:42
  • sounds like either a duplicate of [es6 call static methods](http://stackoverflow.com/q/28627908/1048572)/[Call static method within a class](http://stackoverflow.com/q/31116300/1048572) or [Self-references in object literal declarations](http://stackoverflow.com/q/4616202/1048572) – Bergi Dec 11 '15 at 09:44
  • @Bergi those are stage-1, so not that weird :-) – zerkms Dec 11 '15 at 09:46
  • 1
    @zerkms: Weird in the sense that I really dislike the proposed operator(s). Apparently the choice of the "assignment operator" causes quite some confusion here. – Bergi Dec 11 '15 at 09:58

3 Answers3

12

You can access it as you would expect, however if I recall there were some issues when using Babel and exporting the class immediately, so export after defining the class if you're having problems:

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

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

export default MyStyle;

You can read more about the Babel issue in an answer Marian made on a similar question, which is supposedly fixed in Babel 6.2.1.

Dominic
  • 62,658
  • 20
  • 139
  • 163
1

indeed, this.constructor points to the class hierarchy.

take a look:

class Some {
    static static_elem = 'elem';
    constructor(param) {
         
       console.log( Some.static_elem, param);
        console.log( this.constructor.static_elem, param);
    }
};

var some = new Some('some');

class SomeOther extends Some {
    constructor(param) {
        super(param);
        console.log( Some.static_elem, param);
        console.log( SomeOther.static_elem, param);
        console.log( this.constructor.static_elem, param);
    }
};

var someother = new SomeOther('someother');
alex
  • 651
  • 1
  • 9
  • 11
0
'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)

And const:

'use strict';

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

alert( Menu.elemClass ); // menu

use call to context object --- this

ES6 - Call static method within a class

Community
  • 1
  • 1
Alex Repeckiy
  • 173
  • 10