下面是我在javascript中所做的
class A {
static foo (a, b, callback) {
setTimeout(() => {
let result = doSomethig(a, b)
callback(result)
}, 1000)
}
}
function decorator (OriginalClass) {
return class extends OriginalClass {
static foo (a, b) {
return new Promise((res) => {
super.foo(a, b, (result) => {
res(result)
})
})
}
}
}
@decorator
class B extends A {
}
let result = await B.foo(10, 20)
我想重写一个类的方法,在overwriten方法中,我想调用原始方法。有没有办法用打字机打字?
最佳答案
你可以这样做:
class A {
static bar() {
console.log("barA");
}
}
function decorator(OriginalClass: any) {
let baseBar = OriginalClass.bar;
OriginalClass.bar = function () {
baseBar();
console.log("barOverridden");
}
return OriginalClass;
}
@decorator
class B extends A {
}
B.bar();