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

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

问题描述

目前在ES5中,我们中许多人正在框架中使用以下模式来创建类和类变量,这是可行的:

  // ES 5 
FrameWork.Class({

variable:'string',
variable2:true ,

init:function(){

},

addItem:function(){

}

});

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

  // ES6 
class MyClass {
const MY_CONST ='string'; //< - 这不可能在ES6
constructor(){
this.MY_CONST;
}
}

可惜的是,上面的内容不行,只能包含方法。



我知道我可以在构造函数 this.myVar = true $ c> ...但是我不想垃圾我的构造函数,特别是当我有一个更大的班级有20-30 + params。



我在想许多方法来处理这个问题,但还没有找到任何好的方法。 (例如:创建一个 ClassConfig 处理程序,并传递一个参数对象,它与该类分开声明。处理程序将附加到类,我正在考虑 WeakMaps 也可以集成,不知何故。)



什么样的您将需要处理这种情况?

解决方案

ES维基中关于ES6中提案的注释(最大限度的等级)注意:


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



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



在类定义中指定的属性被分配与它们出现在对象对象中相同的属性


这意味着你所要求的是被考虑的,并明确决定。 / p>

但是...为什么?



好的问题。 TC39的好人希望类声明来声明和定义一个类的功能。不是其成员。 ES6类声明定义其用户的合同。



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

  constructor(){
this.foo = bar
}

在您建议的构造函数中。另请参阅协商一致的摘要



ES7及更高版本



正在开发一种新的ES7提案,使更简洁实例变量通过类声明和表达式 - https://esdiscuss.org/topic/es7-property-initializers


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(){

    }

});

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.

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.

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?

解决方案

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.

This means that what you're asking for was considered, and explicitly decided against.

but... why?

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 and beyond

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