Created
April 23, 2012 07:26
-
-
Save medikoo/2469351 to your computer and use it in GitHub Desktop.
Initial JS for CS showcase can be slightly shorter http://ryanflorence.com/2012/javascript-coffeescript-rewrite/
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
// No function wrapper as it's CommonJS/NodeJS module | |
var slice = [].slice | |
this.events = { | |
events: {}, | |
on: function (topic, handler, context) { | |
(this.events[topic] || (this.events[topic] = [])) | |
.push({ handler: handler, context: context || this }) | |
}, | |
trigger: function (topic) { | |
if (this.events[topic] == null) return | |
var args = slice.call(arguments, 1) | |
for (var event, i = 0; event = this.events[topic][i]; ++i) | |
event.handler.apply(event.context, args) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@rpflorence
this === exports
, so it indeed exports what you expect ;)I just wanted to show that this example was a little biased, that's it. Whether it's good to use CoffeeScript or not, it's totally different story. I'm actually not into it, because I don't see it as replacement but addition (you can't write CoffeeScript without knowing JavaScript), so no matter how simpler code looks like, it's an additional baggage for mind. However if some of the CoffeeScript stuff were to be proposed for EcmaScript 6, I guess I would be very happy with that.