2
'h.collection'에 대한 내 기대치는 [[ 'a', alpha], [ 'b', beta]], 이고 'w.collection'의 경우 [[ 'a' , 알파], [ 'c', gama]].파생 클래스의 Typescript 메서드 데코레이터
그러나 Greeter, Hello 및 World는 모두 동일한 '컬렉션'을 공유합니다. 맞습니까?
그래서 코드를 변경하려면 어떻게해야합니까?
function alias(alias: string) {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
let func = target[propertyKey];
func.alias = alias;
if (!target.collection) {
target.collection = new Map();
}
target.collection.set(alias, func);
};
}
abstract class Greeter {
public collection: Map<string, Function>;
@alias('a')
alpha(){}
}
class Hello extends Greeter {
@alias('b')
beta(){}
}
class World extends Greeter {
@alias('c')
gama(){}
}
let h = new Hello();
let w = new World();
console.log(h.collection);
console.log(w.collection);
이고 tsc 대상은 "es6"입니다. – nsnze