在构造函数中指定类方法功能

时间:2017-08-31 06:36:28

标签: angular typescript ecmascript-6

是否可以在Class构造函数中分配Class方法功能?

例如,我有Button类:

class Button {
  constructor(
    public label: string,
    public icon: string,
    public action: () => void
  ) { }
}

现在我想指定action()的功能,如:

const b = new Button('My button', 'info', () => 2 + 3);

如果我这样做,action()将被创建为空。

更新:上述代码有效。

0 个答案:

没有答案
相关问题