ES6类中的成员变量 [英] Member variables in ES6 classes

查看:122
本文介绍了ES6类中的成员变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用ECMAScript6 表示法来声明一个静态类变量或一个实例变量的默认值?没有 class 我想到的是写为

Is there any way to use the ECMAScript6 class notation to declare either a static class variable or a default value for an instance variable? Without class what I have in mind would be written as

function MyClass(arg) { if(arg) this.arg = arg; }
MyClass.classVariable = 42;
MyClass.prototype.arg = "no arg specified";

我认为最明显的ES6类似符号将是

The most obvious ES6-like notation in my opinion would have been

class MyClass {
    constructor(arg) { if(arg) this.arg = arg; }
    static let classVariable = 42;
    let arg = "no arg specified";
}

但是这不行,因为根据目前的规范草案唯一的 ClassElement 是静态的,实例的方法和分号都是自己的。好的,可以使用一对getter和setter方法来实现与我所概述的类似的语义,但是我猜这是一个严重的性能损失和非常奇怪的语法。

But this doesn't work, since according to the current spec draft the only productions of ClassElement are static and instance methods and semicolons all by themselves. OK, one can use a pair of getter and setter methods to achieve similar semantics to what I outlined, but I guess at a severe performance penalty and with really bizarre syntax.

有没有一些草案建议在符号中包含变量,以某种方式?如果是这样,建议的语法是什么,它在哪里发表,讨论在哪里,讨论如何进行,目前的事态如何?如果没有这样的事情,在任何级别上都没有讨论过,但是我认为不太可能,这个问题是无法回答的。

Is there some draft which suggests including variables in the class notation, one way or another? If so, what was the suggested syntax, where was it published, where was it discussed, how did the discussion go, and what's the current state of affairs on that front? As it stands, this question can't be answered if no such thing has been discussed before, at any level, but I consider that unlikely.

有一点背景:我正在使用使用ES6作为输入的执行高级编译的Google关闭编译器。为了工作,我需要一个地方放置我的类型注释为成员变量,我曾经使用的语法,如 / ** @type {string} * / MyClass.prototype.arg; / code>这是ECMAScript中的一个语义无效,但是向闭包编译器提供的类型信息很好,容易。我还没有找到一个类似的方式,使用构造。但是,如果你关心这个方面,那将是一个评论。上面的问题是关于不是no-ops的成员声明,​​所以这里应该讨论一个答案。

A bit of background: I'm currently toying with the Google closure compiler performing advanced compilation, using ES6 as input. For that to work, I need a place to put my type annotations for member variables, and I used to place them using syntax like /** @type {string} */ MyClass.prototype.arg; which is a semantic no-op in ECMAScript but provides the type information to the closure compiler nice and easy. I haven't yet found a similarly nice way to do this with a class construct. But if you care to address this aspect, that would be a comment. The question above is about member declarations which are more than no-ops, so that's what an answer here should discuss.

推荐答案

ES6几乎肯定不会涵盖定义类变量的语法。只能使用类语法定义方法和getter / setter。这意味着你仍然必须去为类变量的 MyClass.classVariable = 42; 路由。

ES6 will almost certainly not cover syntax for defining class variables. Only methods and getters/setters can be defined using the class syntax. This means you'll still have to go the MyClass.classVariable = 42; route for class variables.

如果您只想使用某些默认值初始化一个类,则可以使用丰富的新的语法集,用于函数参数和可以使用的解构默认值。给一个简单的例子:

If you just want to initialize a class with some defaults, there is a rich new syntax set for function argument and destructuring defaults you can use. To give a simple example:

class Foo {
    constructor(foo = 123) {
        this.foo = foo;
    }
}

new Foo().foo == 123
new Foo(42).foo == 42

这篇关于ES6类中的成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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