简体   繁体   English

嵌套 ES6 类?

[英]Nested ES6 classes?

It seems possible to nest a class in a constructor which can then be instantiated from anywhere within the class, is this official?似乎可以将 class 嵌套在构造函数中,然后可以从 class 中的任何位置实例化,这是官方的吗?

[EDIT] Eg, [编辑] 例如,

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

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 var for 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 {
         …
    }
};

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.Node
  • Node {symbol: 0, weight: 0}
  • new Huffman.Node
  • Node {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 Note The above construction breaks instanceof for Node (why? because a whole new class is defined with every get call).请注意上述构造破坏了Node instanceof (为什么?因为每次 get 调用都定义了一个全新的类)。 To not break instanceof define Node outside of the scope of a single getter, either in the constructor (disabling the Huffman.Node class property and causing instanceof to 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 instanceof to work in all scopes below that the one where Node is defined).为了不破坏instanceof在单个 getter 的范围之外定义 Node,或者在构造函数中(禁用 Huffman.Node 类属性并导致instanceof在单个 Huffman 实例的命名空间内工作,并在该命名空间之外中断),或者定义 Node在 Huffman 的同级或祖先作用域中(允许instanceof在定义 Node 的作用域以下的所有作用域中工作)。

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

When you create a nested child class in a constructor of a parent class, this means every instance of the parent class has its own child class.当您在父 class 的构造函数中创建嵌套子 class 时,这意味着父 class 的每个实例都有自己的子 ZA2F21ED4F8EBC2CBBDC4 Typically this is not what you want.通常这不是您想要的。 Instead you want a child class, which is shared among all instances of the parent class.相反,您需要一个子 class,它在父 class 的所有实例之间共享。 This means the nested class must be static.这意味着嵌套的 class 必须是 static。 This is an example:这是一个例子:

 class Parent { static Child = class Child { constructor (name) { console.log (`Child: ${name}`); } } constructor (...names) { console.log ('Parent'); this.children = names.map (name => new Parent.Child (name)); } } var p = new Parent ('Alice', 'Bob'); console.log (`same type? ${p.children[0].constructor === p.children[1].constructor}`);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM