我可以做一些类似于 CoffeeScript 或 Ruby 的事情吗,我可以在其中创建类-“宏”
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
这将创建以下 Javascript 代码
var A;
A = (function() {
function A() {}
events(A);
A.listenTo(SomeEvent);
A.prototype.onSomeEvent = function(event) {};
return A;
})();