Javascript ES6 类中的成员变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28308578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Member variables in ES6 classes
提问by MvG
Is there any way to use the ECMAScript6 classnotation to declare either a static class variable or a default value for an instance variable? Without classwhat I have in mind would be written as
有没有办法使用 ECMAScript6class表示法来声明静态类变量或实例变量的默认值?没有class我的想法会被写成
function MyClass(arg) { if(arg) this.arg = arg; }
MyClass.classVariable = 42;
MyClass.prototype.arg = "no arg specified";
The most obvious ES6-like notation in my opinion would have been
在我看来,最明显的类似 ES6 的符号是
class MyClass {
constructor(arg) { if(arg) this.arg = arg; }
static let classVariable = 42;
let arg = "no arg specified";
}
But this doesn't work, since according to the current spec draftthe only productions of ClassElementare 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.
但这不起作用,因为根据当前的规范草案,ClassElement的唯一产品是静态和实例方法以及分号本身。好的,可以使用一对 getter 和 setter 方法来实现与我概述的相似的语义,但我猜会严重的性能损失和非常奇怪的语法。
Is there some draft which suggests including variables in the classnotation, 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.
是否有一些草案建议以class一种或另一种方式在符号中包含变量?如果是这样,建议的语法是什么,在哪里发布,在哪里讨论,讨论如何进行,以及这方面的当前状况如何?就目前而言,如果之前没有在任何级别讨论过这样的事情,就无法回答这个问题,但我认为这不太可能。
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 classconstruct. 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.
一点背景知识:我目前正在玩弄执行高级编译的 Google 闭包编译器,使用 ES6 作为输入。为此,我需要一个地方来放置我的成员变量的类型注释,我曾经使用这样的语法来放置它们/** @type {string} */ MyClass.prototype.arg;,这是 ECMAScript 中的语义无操作,但为闭包编译器提供了类型信息,非常简单。我还没有找到一种类似的好方法来使用class构造来做到这一点。但如果你想解决这个问题,那将是一个评论。上面的问题是关于成员声明不仅仅是无操作,所以这就是这里应该讨论的答案。
回答by lyschoening
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.
ES6 几乎肯定不会涵盖定义类变量的语法。只能使用类语法定义方法和 getter/setter。这意味着您仍然必须走MyClass.classVariable = 42;类变量的路线。
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
回答by ericsoco
I haven't used Google Closure Compiler, but with Babel you can declare static(scoped to a class) variables as described here. The article is focused on React because of the utility of staticmembers for React, but is applicable to ES6 classes in general.
我没有使用过 Google Closure Compiler,但是使用 Babel,您可以按照此处所述声明static(范围为 a class)变量。由于成员对 React的效用,本文重点介绍 React ,但一般适用于 ES6 es。staticclass
Syntax is close to your proposed syntax:
语法接近您建议的语法:
class MyClass {
constructor(arg) { if(arg) this.arg = arg; }
static defaultArg = 42;
let arg = MyClass.defaultArg;
}
Note that you'll have to add 'es7.classProperties'to your .babelrcfor this to compile. See the Babel 5.0.0 release notesfor more info.
请注意,您必须添加'es7.classProperties'到您.babelrc的才能编译。有关更多信息,请参阅Babel 5.0.0 发行说明。
I don't know if there's a way to declare a staticas a const.
我不知道是否有办法将 a 声明static为 a const。
回答by Clayton Gulick
While it's not part of the ES6 spec, this looks like it's coming soon and is already supported by Babel and some others.
虽然它不是 ES6 规范的一部分,但它看起来很快就会出现并且已经被 Babel 和其他一些支持。
Here's the spec: https://github.com/jeffmo/es-class-fields-and-static-properties
这是规范:https: //github.com/jeffmo/es-class-fields-and-static-properties
And a full list of all the proposals and their status: https://github.com/tc39/ecma262
以及所有提案及其状态的完整列表:https: //github.com/tc39/ecma262

