将另一个类作为类的静态属性

时间:2015-07-23 13:17:47

标签: javascript class ecmascript-6

请阅读下面的示例*,但请不要过多关注Date is 2015-02-16 继承,请 - 它只显示EventEmitter语法的效用。

我意识到这个例子不正确ES2015,因为没有class语句这样的东西。

在ES2015中制作类似这样的作品的语法最精益的方法是什么?

static class

我应该将它拆分并使用类表达式,如下所示?看起来不太优雅。

class App extends EventEmitter {
  addPage(name) {
    this[name] = new App.Page;
    this.emit("page-added");
  }

  static class Page extends EventEmitter {
    constructor() {
      super();
      this._paragraphs = [];
    }

    addParagraph(text) {
      this._paragraphs.push(text);
      this.emit("paragraph-added");
    }
  }
}

2 个答案:

答案 0 :(得分:1)

  

我应该将其拆分并使用类表达式吗?

是的,这是要走的路。如果您坚持使用声明,则必须在之后进行Option Explicit Sub main() Call for_each_in_others(rDATA:=Worksheets("Sheet3").Range("A3"), bHDR:=True) End Sub Sub for_each_in_others(rDATA As Range, Optional bHDR As Boolean = False) Dim v As Long, w As Long Dim iINCROWS As Long, iMAXROWS As Long, sErrorRng As String Dim vVALs As Variant, vTMPs As Variant, vCOLs As Variant On Error GoTo bm_Safe_Exit appTGGL bTGGL:=False With rDATA.Parent With rDATA(1).CurrentRegion 'Debug.Print rDATA(1).Row - .Cells(1).Row With .Resize(.Rows.Count - (rDATA(1).Row - .Cells(1).Row), .Columns.Count).Offset(2, 0) sErrorRng = .Address(0, 0) vTMPs = .Value2 ReDim vCOLs(LBound(vTMPs, 2) To UBound(vTMPs, 2)) iMAXROWS = 1 'On Error GoTo bm_Output_Exceeded For w = LBound(vTMPs, 2) To UBound(vTMPs, 2) vCOLs(w) = Application.CountA(.Columns(w)) iMAXROWS = iMAXROWS * vCOLs(w) Next w 'control excessive or no rows of output If iMAXROWS > Rows.Count Then GoTo bm_Output_Exceeded ElseIf .Columns.Count = 1 Or iMAXROWS = 0 Then GoTo bm_Nothing_To_Do End If On Error GoTo bm_Safe_Exit ReDim vVALs(LBound(vTMPs, 1) To iMAXROWS, LBound(vTMPs, 2) To UBound(vTMPs, 2)) iINCROWS = 1 For w = LBound(vVALs, 2) To UBound(vVALs, 2) iINCROWS = iINCROWS * vCOLs(w) For v = LBound(vVALs, 1) To UBound(vVALs, 1) vVALs(v, w) = vTMPs((Int(iINCROWS * ((v - 1) / UBound(vVALs, 1))) Mod vCOLs(w)) + 1, w) Next v Next w End With End With .Cells(2, UBound(vVALs, 2) + 2).Resize(1, UBound(vVALs, 2) + 2).EntireColumn.Delete If bHDR Then rDATA.Cells(1, 1).Offset(-1, 0).Resize(1, UBound(vVALs, 2)).Copy _ Destination:=rDATA.Cells(1, UBound(vVALs, 2) + 2).Offset(-1, 0) End If rDATA.Cells(1, UBound(vVALs, 2) + 2).Resize(UBound(vVALs, 1), UBound(vVALs, 2)) = vVALs End With GoTo bm_Safe_Exit bm_Nothing_To_Do: MsgBox "There is not enough data in " & sErrorRng & " to perform expansion." & Chr(10) & _ "This could be due to a single column of values or one or more blank column(s) of values." & _ Chr(10) & Chr(10) & "There is nothing to expand.", vbInformation, _ "Single or No Column of Raw Data" GoTo bm_Safe_Exit bm_Output_Exceeded: MsgBox "The number of expanded values created from " & sErrorRng & _ " (" & Format(iMAXROWS, "\> #, ##0") & " rows × " & UBound(vTMPs, 2) & _ " columns) exceeds the rows available (" & Format(Rows.Count, "#, ##0") & ") on this worksheet.", vbCritical, _ "Too Many Entries" bm_Safe_Exit: appTGGL End Sub Sub appTGGL(Optional bTGGL As Boolean = True) Application.EnableEvents = bTGGL Application.ScreenUpdating = bTGGL End Sub 作业。

答案 1 :(得分:0)

您可以在addPage内创建班级。它将创建"抽象"我想你正在寻找,但你会在性能上付费(每次addPage通话都会变慢。



'use strict';

class EventEmitter {
  emit(s) {
    alert(s);
  }
}

class App extends EventEmitter {
  addPage(name) {
    class Page extends EventEmitter {
      constructor() {
        super();
        this._paragraphs = [];
      }

      addParagraph(text) {
        this._paragraphs.push(text);
        this.emit("paragraph-added");
      }
    }
    this[name] = new Page;
    this.emit("page-added");
  }
}

var app = new App;
app.addPage("myPage");
app.myPage.addParagraph("Some content");




或者,在模块中定义两个类,并仅导出App类,从而防止污染全局范围。