ES6 类变量替代方案 [英] ES6 class variable alternatives

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

问题描述

目前在 ES5 中,我们很多人在框架中使用以下模式来创建类和类变量,这很舒服:

Currently in ES5 many of us are using the following pattern in frameworks to create classes and class variables, which is comfy:

// ES 5
FrameWork.Class({

    variable: 'string',
    variable2: true,

    init: function(){

    },

    addItem: function(){

    }

});

在 ES6 中,您可以本地创建类,但没有选择类变量的选项:

In ES6 you can create classes natively, but there is no option to have class variables:

// ES6
class MyClass {
    const MY_CONST = 'string'; // <-- this is not possible in ES6
    constructor(){
        this.MY_CONST;
    }
}

遗憾的是,上面的方法不起作用,因为类只能包含方法.

Sadly, the above won't work, as classes only can contain methods.

我知道我可以在 constructorthis.myVar = true ......但我不想垃圾"我的构造函数,尤其是当我有 20-30+ 更大类的参数.

I understand that I can this.myVar = true in constructor…but I don't want to 'junk' my constructor, especially when I have 20-30+ params for a bigger class.

我想了很多方法来处理这个问题,但还没有找到任何好的方法.(例如:创建一个 ClassConfig 处理程序,并传递一个 parameter 对象,该对象与类分开声明.然后处理程序将附加到类.我在想WeakMaps 也以某种方式集成.)

I was thinking of many ways to handle this issue, but haven't yet found any good ones. (For example: create a ClassConfig handler, and pass a parameter object, which is declared separately from the class. Then the handler would attach to the class. I was thinking about WeakMaps also to integrate, somehow.)

你有什么样的想法来处理这种情况?

What kind of ideas would you have to handle this situation?

推荐答案

2018 更新:

现在有一个第 3 阶段的提案 - 我期待在几个月内使这个答案过时.

There is now a stage 3 proposal - I am looking forward to make this answer obsolete in a few months.

与此同时,任何使用 TypeScript 或 babel 的人都可以使用以下语法:

In the meantime anyone using TypeScript or babel can use the syntax:

varName = value

在类声明/表达式主体中,它将定义一个变量.希望在几个月/几周后,我能够发布更新.

Inside a class declaration/expression body and it will define a variable. Hopefully in a few months/weeks I'll be able to post an update.

更新:Chrome 74 现在可以使用此语法.

Update: Chrome 74 now ships with this syntax working.

ES wiki 中关于 ES6 提案的注释(最大最小类) 注意:

The notes in the ES wiki for the proposal in ES6 (maximally minimal classes) note:

(有意)没有直接的声明方式来定义原型数据属性(方法除外)类属性或实例属性

There is (intentionally) no direct declarative way to define either prototype data properties (other than methods) class properties, or instance property

类属性和原型数据属性需要在声明之外创建.

Class properties and prototype data properties need be created outside the declaration.

在类定义中指定的属性被赋予与出现在对象字面量中相同的属性.

Properties specified in a class definition are assigned the same attributes as if they appeared in an object literal.

这意味着您的要求已被考虑,并明确决定反对.

好问题.TC39 的好人希望类声明来声明和定义类的功能.不是它的成员.ES6 类声明为其用户定义了它的契约.

Good question. The good people of TC39 want class declarations to declare and define the capabilities of a class. Not its members. An ES6 class declaration defines its contract for its user.

记住,类定义定义了原型方法——在原型上定义变量通常不是你要做的.你当然可以使用:

Remember, a class definition defines prototype methods - defining variables on the prototype is generally not something you do. You can, of course use:

constructor(){
    this.foo = bar
}

在您建议的构造函数中.另请参阅 共识总结.

In the constructor like you suggested. Also see the summary of the consensus.

一项针对 ES7 的新提案正在制定中,该提案允许通过类声明和表达式实现更简洁的实例变量 - https://esdiscuss.org/topic/es7-property-initializers

A new proposal for ES7 is being worked on that allows more concise instance variables through class declarations and expressions - https://esdiscuss.org/topic/es7-property-initializers

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

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