¿Puedo acceder a la "clase de objeto" desde dentro del cuerpo de la clase a máquina de escribir

votos
2

¿Puedo hacer algo similar a CoffeeScript o Ruby donde puedo crear macros de clase

class A
    # events adds the class method listenTo to the class (not to the prototype)
    # listenTo will make all instances of A a listener to the given Event
    events @

    # this will register instances of A to listen for SomeEvents
    # the event broker (not here in this code) will specifically look
    # for a method called onSomeEvent(event)
    @listenTo SomeEvent

    # and then later
    onSomeEvent: (event)-> #do what ever is needed

Esto creará el siguiente código Javascript

var A;
A = (function() {
   function A() {}
   events(A);
   A.listenTo(SomeEvent);
   A.prototype.onSomeEvent = function(event) {};
   return A;
})();
Publicado el 02/10/2012 a las 08:41
fuente por usuario
En otros idiomas...                            


1 respuestas

votos
2

En cuanto a su ejemplo, si se escribe esto:

function events(A:any) {
    A.listenTo = function(arg:any){alert(arg);};
}

class A {
    public onSomeEvent(event:any) {
        //do stuff  
    }
    constructor {
        events(A);
        (<any>A).listenTo("SomeEvent");
    }
}

a máquina, se compilará en:

function events(A) {
    A.listenTo = function (arg) {
        alert(arg);
    };
}
var A = (function () {
    function A() {
        events(A);
        (A).listenTo("SomeEvent");
    }
    A.prototype.onSomeEvent = function (event) {
    };
    return A;
})();
Respondida el 03/10/2012 a las 23:43
fuente por usuario

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more