JS& ES6:从类中访问静态字段 [英] JS & ES6: Access static fields from within class

查看:90
本文介绍了JS& ES6:从类中访问静态字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ES6中,给出以下示例:

In ES6, given the following example:

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

如何访问Color.mainDark(静态字段)?

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

推荐答案

我认为你的意思是ES.next(可能使用babel stage-0),因为klaemo说你可以按照你的预期访问它,但是如果我记得有一些使用Babel并立即导出类时出现问题,因此在定义类后导出如果您遇到问题:

I assume you mean ES.next (probably using babel stage-0) as klaemo said 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;

您可以在 Marian根据类似问题做出的回答,据推测在Babel 6.2.1中确定。

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.

这篇关于JS& ES6:从类中访问静态字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆