Mohu přistupovat k „objekt třídy“ z uvnitř těla třídy na psacím stroji

hlasů
2

Mohu udělat něco podobného na CoffeeScript nebo Ruby, kde bych mohl vytvořit tř „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

Tím se vytvoří následující kód JavaScript

var A;
A = (function() {
   function A() {}
   events(A);
   A.listenTo(SomeEvent);
   A.prototype.onSomeEvent = function(event) {};
   return A;
})();
Položena 02/10/2012 v 08:41
zdroj uživatelem
V jiných jazycích...                            


1 odpovědí

hlasů
2

Při pohledu na svůj Například pokud píšete toto:

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

na psacím stroji, bude sestavovat do:

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;
})();
Odpovězeno 03/10/2012 v 23:43
zdroj uživatelem

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