如何制作“公共静态字段"?在 ES6 类中? [英] How do I make a "public static field" in an ES6 class?

查看:13
本文介绍了如何制作“公共静态字段"?在 ES6 类中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 Javascript 类,并且我想要一个像 Java 中那样的公共静态字段.这是相关代码:

导出默认类代理{圈:1,平方:2,...

这是我得到的错误:

第 2 行,第 11 栏,类属性必须是方法.预期 '(' 但看到的是 ':'.

看起来 ES6 模块不允许这样做.有没有办法获得所需的行为,还是我必须编写一个 getter?

解决方案

您使用访问器和静态"关键字创建公共静态字段":

class Agent {静态获取 CIRCLE() {返回 1;}静态获取 SQUARE() {返回2;}}代理.CIRCLE;//1

查看规范,14.5 — 类定义 — 您d 看到一些可疑的相关内容 :)

<块引用>

ClassElement[产量]:
  MethodDefinition[?Yield]
  静态 MethodDefinition[?Yield] ;

所以从那里你可以跟随 14.5.14— 运行时语义:ClassDefinitionEvaluation — 仔细检查它是否真的像它看起来那样做.具体来说,第 20 步:

<块引用>

  1. 对于每个 ClassElement m 按顺序从方法

    1. 如果 IsStatic of m 为 false,则

      1. 让 status 是对 m 执行 PropertyDefinitionEvaluation 的结果,参数为 proto 和 false.

    2. 否则,

      1. 让 status 是对 m 执行 PropertyDefinitionEvaluation 的结果,参数为 F 和 false.

    3. 如果状态是突然完成,那么

      1. 将运行执行上下文的 LexicalEnvironment 设置为 lex.
      2. 返回状态.

IsStatic 早在 14.5.9 中定义><块引用>

ClassElement:静态方法定义
返回真.

所以 PropertyMethodDefinition 以F"(构造函数,函数对象)作为参数被调用,而后者又在该对象上创建访问器方法.

至少在 IETP(技术预览版)以及 6to5 和 Traceur 中已经工作编译器.

I'm making a Javascript class and I'd like to have a public static field like in Java. This is the relevant code:

export default class Agent {
    CIRCLE: 1,
    SQUARE: 2,
    ...

This is the error I get:

line 2, col 11, Class properties must be methods. Expected '(' but instead saw ':'.

It looks like ES6 modules don't allow this. Is there a way to get the desired behavior or do I have to write a getter?

解决方案

You make "public static field" using accessor and a "static" keyword:

class Agent {
    static get CIRCLE() {
      return 1;
    }
    static get SQUARE() {
      return 2;
    }
}

Agent.CIRCLE; // 1

Looking at a spec, 14.5 — Class Definitions — you'd see something suspiciously relevant :)

ClassElement[Yield] :
  MethodDefinition[?Yield]
  static MethodDefinition[?Yield] ;

So from there you can follow to 14.5.14 — Runtime Semantics: ClassDefinitionEvaluation — to double check if it really does what it looks like it does. Specifically, step 20:

  1. For each ClassElement m in order from methods

    1. If IsStatic of m is false, then

      1. Let status be the result of performing PropertyDefinitionEvaluation for m with arguments proto and false.

    2. Else,

      1. Let status be the result of performing PropertyDefinitionEvaluation for m with arguments F and false.

    3. If status is an abrupt completion, then

      1. Set the running execution context’s LexicalEnvironment to lex.
      2. Return status.

IsStatic is defined earlier in 14.5.9

ClassElement : static MethodDefinition
Return true.

So PropertyMethodDefinition is called with "F" (constructor, function object) as an argument, which in its turn creates an accessor method on that object.

This already works in at least IETP (tech preview), as well as 6to5 and Traceur compilers.

这篇关于如何制作“公共静态字段"?在 ES6 类中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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