ES6类,成员属性定义为静态/共享 [英] ES6 classes, member properties definitions as static/shared
问题描述
我使用io.js测试ES 6中的类2.xx
下面的例子我从Mozilla,
获得的东西在轨道上(OOp在JS),
至少我们现在有直接继承(在语法级)
'extends'指令...
我提出的问题是成员属性在构造函数内部定义
这是至少一个语法问题...
(通过网络搜索,并发现很少有关这方面的信息)
将是一个更大的问题,当ESxx尝试有属性成员的可见性指令(在不久的将来,我猜)
无论如何,现在...
如何声明一个共享/静态属性?
// Mozilla中的示例
类Polygon
{
构造函数
{
this.name ='Polygon';
this.height = height;
this.width = width;
}
}
类方形扩展多边形
{
constructor(length)
{
super(length,length) ;
this.name ='Square';
}
}
可以用 getters 定义静态或原型属性:
class Foo {
static get bar (){
return 42;
}
get bar(){
return 21;
}
}
这不太理想, >
I am testing classes in ES 6 with io.js 2.xx the example below I took from Mozilla, Things are getting on tracks (OOp in JS), at least we now have direct inheritance (at syntax level) with the 'extends' directive... the problem I pose is that member properties are defined inside constructor this is at least a syntax problem... (been searched through the web and found very few information about this) will be more a of a problem when ESxx try to had visibility directives to property members (in a near future I guess)
Anyway, for now... How do I declare a shared/static property?
// example from Mozilla
class Polygon
{
constructor(height, width)
{
this.name = 'Polygon';
this.height = height;
this.width = width;
}
}
class Square extends Polygon
{
constructor(length)
{
super(length, length);
this.name = 'Square';
}
}
You can define static or prototype properties with getters:
class Foo {
static get bar() {
return 42;
}
get bar() {
return 21;
}
}
It's not ideal, but it works.
这篇关于ES6类,成员属性定义为静态/共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!