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

查看:1329
本文介绍了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 的唯一生成是静态和实例方法和分号全部自己。 OK,可以使用一对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.

一点背景:我目前正在用Google闭包编译器执行高级编译,使用ES6作为输入。为了工作,我需要一个地方为成员变量放我的类型注释,我曾经使用像 / ** @type {string} * / MyClass.prototype.arg; / code>这是一个在ECMAScript中的语义无操作,但提供类型信息给闭包编译器漂亮和容易。我还没有找到一个类似的好方法做一个构造。但如果你关心这个方面,那将是一个评论。

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天全站免登陆