-
-
Save wilmoore/3719959 to your computer and use it in GitHub Desktop.
CoffeeScript is clean, but the generated JS is...generated.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@events = | |
events: {} | |
on: (topic, handler, context = this) -> | |
(@events[topic] or= []).push {handler, context} | |
trigger: (topic, args...) -> | |
return unless @events[topic]? | |
handler.apply(context, args) for {handler, context} in @events[topic] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var __slice = [].slice; | |
this.events = { | |
events: {}, | |
on: function(topic, handler, context) { | |
var _base; | |
if (context == null) { | |
context = this; | |
} | |
return ((_base = this.events)[topic] || (_base[topic] = [])).push({ | |
handler: handler, | |
context: context | |
}); | |
}, | |
trigger: function() { | |
var args, context, handler, topic, _i, _len, _ref, _ref1, _results; | |
topic = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; | |
if (this.events[topic] == null) { | |
return; | |
} | |
_ref = this.events[topic]; | |
_results = []; | |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
_ref1 = _ref[_i], handler = _ref1.handler, context = _ref1.context; | |
_results.push(handler.apply(context, args)); | |
} | |
return _results; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var slice = Array.prototype.slice; | |
this.events = { | |
events: {}, | |
on: function (topic, handler, context) { | |
if (context == null) context = this; | |
this.events[topic] = this.events[topic] || [] | |
this.events[topic].push({ handler: handler, context: context }); | |
}, | |
trigger: function (topic) { | |
if (this.events[topic] == null) return; | |
var args = slice.apply(arguments, 1); | |
for (var i = 0, l = this.events[topic].length, event; i < l; i++) { | |
event = this.events[topic][i]; | |
event.handler.apply(event.context, args); | |
} | |
} | |
}; | |
}).call(this); |
True , the generated javascript is harder to read, i call it the "babylon" effect , like the tower in the first testament. But it is still readable.However there is no 1 to 1 coffee - js translation so , though the cs is compact , i would not have gone for such an extreme "optimisation".
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nobody would purposely write JS to match what was generated. Further, the CS version isn't that much more terse than a cleanly written JS version.
I like the idea behind CoffeeScript, but in reality, the generated code is a mess. Not sure why I was expecting anything more to be honest.