Mogę wejść do klasy „obiekt” z wewnątrz ciała klasy w maszynopisie

głosy
2

Mogę zrobić coś podobnego do Ruby coffeescript lub gdzie mogę tworzyć wykładowa „makra”

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

Spowoduje to utworzenie następujący kod Javascript

var A;
A = (function() {
   function A() {}
   events(A);
   A.listenTo(SomeEvent);
   A.prototype.onSomeEvent = function(event) {};
   return A;
})();
Utwórz 02/10/2012 o 08:41
źródło użytkownik
W innych językach...                            


1 odpowiedzi

głosy
2

Patrząc na przykład jeśli piszesz tak:

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");
    }
}

w maszynopisie, będzie kompilować na:

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;
})();
Odpowiedział 03/10/2012 o 23:43
źródło użytkownik

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