最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS & ES6: Access static fields from within class - Stack Overflow

programmeradmin1浏览0评论

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)?

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)?

Share Improve this question edited Sep 30, 2017 at 17:49 Felix Kling 817k180 gold badges1.1k silver badges1.2k bronze badges asked Dec 11, 2015 at 9:28 LiviosoLivioso 1,2521 gold badge12 silver badges23 bronze badges 6
  • 2 You cannot. It's not defined until the closing } on the last line – zerkms Commented Dec 11, 2015 at 9:32
  • 1 in a method you could do MyStyle.Color.mainDark. – klaemo Commented Dec 11, 2015 at 9:33
  • 5 That's not ES6. That's some weird experimental (ES7-proposed) property intialisers. – Bergi Commented Dec 11, 2015 at 9:42
  • sounds like either a duplicate of es6 call static methods/Call static method within a class or Self-references in object literal declarations – Bergi Commented Dec 11, 2015 at 9:44
  • 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 Commented Dec 11, 2015 at 9:58
 |  Show 1 more comment

3 Answers 3

Reset to default 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.

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');
'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

发布评论

评论列表(0)

  1. 暂无评论