【发布时间】:2015-08-13 23:53:24
【问题描述】:
有没有办法追溯(即在已经定义类之后)向 ES6 类添加实例方法?
考虑以下类:
class Thing {}
我现在想将hello 方法附加到Thing,然后可以像这样在其实例上调用:
let thing = new Thing();
thing.hello();
有可能吗?
(当然,我可以创建一个子类,但这不是我在这里要问的。)
【问题讨论】:
-
原型继承的工作方式与 ES5 中的相同。只需添加另一个属性。添加一个 method (在 ES6 意义上),使其工作方式与在类文字中声明的一样是 a bit more complicated。
标签: javascript class methods ecmascript-6