以下是我在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)
我想覆盖一个类的方法,并且在覆盖的方法中,我想调用原始方法。在打字稿中有什么办法吗?
答案 0 :(得分:0)
您可以这样做:
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();
答案 1 :(得分:0)
function StaticMethodDecorator(
target: Function, // the function itself and not the prototype
propertyKey: string | symbol, // The name of the static method
descriptor: TypedPropertyDescriptor<any>
) {
console.log("StaticMethodDecorator called on: ", target, propertyKey, descriptor);
}
class StaticMethodDecoratorExample {
@StaticMethodDecorator
static staticMethod() {
}
}
如以下描述:https://khru.gitbooks.io/typescript/static_method_decorator.html