Javascript 嵌套的 ES6 类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28784375/
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
Nested ES6 classes?
提问by user5321531
It seems possible to nest a class in a constructor which can then be instantiated from anywhere within the class, is this official?
似乎可以在构造函数中嵌套一个类,然后可以从类中的任何地方实例化,这是官方的吗?
[EDIT] E.g.,
[编辑] 例如,
class C {
constructor() {
class D {
constructor() { }
}
}
method() {
var a = new D(); // works fine
}
}
//var a = new D(); // fails in outer scope
The traceur generated JS https://google.github.io/traceur-compiler/demo/repl.html
traceur 生成的 JS https://google.github.io/traceur-compiler/demo/repl.html
$traceurRuntime.ModuleStore.getAnonymousModule(function() {
"use strict";
var C = function C() {
var D = function D() {};
($traceurRuntime.createClass)(D, {}, {});
};
($traceurRuntime.createClass)(C, {method: function() {
var a = new D();
}}, {});
return {};
});
//# sourceURL=traceured.js
回答by Bergi
No, there are no nested classes in ES6, and there is no such thing as private members in the class syntax anyway if you mean that.
不,在 ES6 中没有嵌套类,并且无论如何在类语法中都没有私有成员这样的东西,如果你是这个意思的话。
Of course you can put a second class as a static property on another class, like this:
当然,您可以将第二个类作为另一个类的静态属性,如下所示:
class A {
…
}
A.B = class {
…
};
or you use an extra scope:
或者您使用额外的范围:
var C;
{
class D {
constructor() { }
}
C = class C {
constructor() { }
method() {
var a = new D(); // works fine
}
}
}
(There seems to be a bug with traceur as it uses a hoisted varfor the class declaration instead of block scope)
( traceur 似乎存在一个错误,因为它使用提升var的类声明而不是块作用域)
With the proposed class field syntax, it will also be possible to write a single expression or declaration:
使用建议的类字段语法,也可以编写单个表达式或声明:
class A {
…
static B = class {
…
}
};
回答by Ammatwain
something like that?
类似的东西?
class A {
constructor () {
this.B = class {
echo () {
console.log('I am B class');
}
}
}
echo () {
this.b = new this.B;
this.b.echo();
}
}
var a = new A;
a.echo();
回答by user2530580
You could use a getter:
您可以使用吸气剂:
class Huffman {
constructor() { /* ... */ }
static get Node() {
return class Node {
constructor() {
var API = this;
API.symbol = 0; API.weight = 0;
return API;
}
};
}
get Node() {
return Huffman.Node;
}
encode() { /* ... */ }
decode() { /* ... */ }
/* ... */
}
// usage
huffman = new Huffman;
new huffman.Node;
new Huffman.Node;
Which in latest Chrome Dev 44.0.2376.0 on Apple 10.10.2 gives in console
Apple 10.10.2 上最新的 Chrome Dev 44.0.2376.0 在控制台中给出
new huffman.NodeNode {symbol: 0, weight: 0}new Huffman.NodeNode {symbol: 0, weight: 0}
new huffman.NodeNode {symbol: 0, weight: 0}new Huffman.NodeNode {symbol: 0, weight: 0}
In other news, getters are the secret sauce that let's you do a whole bunch of cool things in ES6.
在其他新闻中,getter 是让你在 ES6 中做一大堆很酷的事情的秘密武器。
Please NoteThe above construction breaks instanceoffor Node(why? because a whole new class is defined with every get call). To not break instanceofdefine Node outside of the scope of a single getter, either in the constructor (disabling the Huffman.Node class property and causing instanceofto work within the namespace of a single Huffman instance, and break outside that), or define Node in a sibling or ancestor scope to Huffman (allowing instanceofto work in all scopes below that the one where Node is defined).
请注意,上述结构中断instanceof了Node(为什么?因为一个全新的类与每个get调用定义)。为了不在instanceof单个 getter 的范围之外中断定义 Node,或者在构造函数中(禁用 Huffman.Node 类属性并导致instanceof在单个 Huffman 实例的命名空间内工作,并在该名称空间之外中断),或者在一个Huffman 的兄弟或祖先作用域(允许instanceof在定义 Node 的作用域以下的所有作用域中工作)。

