Skip to content

Instantly share code, notes, and snippets.

@medikoo
Created April 23, 2012 07:26
Show Gist options
  • Save medikoo/2469351 to your computer and use it in GitHub Desktop.
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/
// 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)
}
}
@medikoo
Copy link
Author

medikoo commented Apr 23, 2012

@rpflorence

  1. Not really, JavaScript is not only in browsers, but also server-side. This is the way you write code for Node.js, and you can write same way for browsers with help of tools like webmake - That's how I work for over a year right now ...and other thing, in module: this === exports, so it indeed exports what you expect ;)
  2. I just shown, that in many cases you may write similar constructs as in CoffeeScript. If you don't like it because it's doing too much at once you should in first place refrain from using CoffeScript, as it's how it works just one magnitude larger.
  3. Experienced JavaScript programmers know and use that construct.There's nothing unusual about that.
  4. You are exaggerating, and I guess you know that. Many great JavaScript projects don't use semicolons, and to the point - it's definitely not more dangerous than using CoffeeScript.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment