2017-01-06 1 views
0

메소드 장식자를 개발하는 것이 가능할 수도 있습니다. 즉, 이 클래스의 프로토 타입을 수정하는 "@rpcMethod"입니까? 끝에 클래스를 수정하는 메소드 데코레이터?

내가 원하는 : 나는 장식 내부 것들 모든 종류의 시도
instance.getRpcMethods(); //returns ['get'] 

(예 : target.prototype 수정)하지만 모든
@rpcMethod 
get(){ 
    .... 
} 

및 실행시

실패한.

감사합니다. 답변이 늦어 죄송합니다

+0

당신이 클래스 장식을 사용할 수 있다는 사실을 알고 계십니까? – Alex

+0

예,하지만 아직 런타임에 '@rpcMethod'로 장식 된 모든 메서드를 수집하는 방법을 모르겠습니다. – xamiro

답변

1

,이 시도 :

var rpcMethod = (target: Object, propName: string, propertyDescriptor: TypedPropertyDescriptor<() => any>) => { 
    var desc = Object.getOwnPropertyDescriptor(target, "getRpcMethods"); 
    if (desc.configurable) 
    { 
     Object.defineProperty(target, "getRpcMethods", { value: function() { return this["rpcMethods"]; }, configurable: false }); 
     Object.defineProperty(target, "rpcMethods", { value: [] }); 
    } 

    target["rpcMethods"].push(propName); 
}; 


class MyClass 
{ 
    public getRpcMethods():string[] { 
     throw new Error("Should be implemented by decorator"); 
    } 

    @rpcMethod 
    public get() { 
     return "x"; 
    } 

    @rpcMethod 
    public post() { 
     return "y"; 
    } 
} 

var myInstance = new MyClass(); 

myInstance.getRpcMethods(); 
+0

안녕하세요. 환상적입니다. 정말 고맙습니다. – xamiro

관련 문제