El tudom érni az „osztály objektum” belül az osztály test írógéppel

szavazat
2

Tehetek valamit hasonló CoffeeScript vagy Ruby ahol tudok létrehozni osztály- „makrók”

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

Ez hozza létre a következő JavaScript kódot

var A;
A = (function() {
   function A() {}
   events(A);
   A.listenTo(SomeEvent);
   A.prototype.onSomeEvent = function(event) {};
   return A;
})();
A kérdést 02/10/2012 08:41
a forrás felhasználó
Más nyelveken...                            


1 válasz

szavazat
2

Nézzük meg például, ha írsz a következő:

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

géppel, akkor fordítsd be:

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;
})();
Válaszolt 03/10/2012 23:43
a forrás felhasználó

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