【发布时间】:2019-03-11 05:51:18
【问题描述】:
我想在方法中添加一个方法:
class MyClass {
public foo(text: string): string {
return text + ' FOO!'
}
// Some magical code which adds the method `bar` to `foo`.
}
const obj = new MyClass();
console.log(obj.foo('thing'));
console.log(obj.foo.bar('other thing'));
这可能吗?我为函数找到了similar case:
function Sum(x: number) { /*...*/ }
namespace Sum {
export function empty() { /*...*/ }
}
有没有办法对类的方法做同样的事情?
我希望在类中包含代码,而不是在创建对象后进行猴子补丁。
【问题讨论】:
标签: javascript typescript methods