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)
}
}
@ryanflorence
Copy link

  1. You can't just drop the wrapper, that's a common pattern you should do for every file for the browser (also, you aren't exporting anything so the code won't work for node, btw :)
  2. That line in the on method is doing too much all at once for my tastes, and splitting it up into two lines doesn't make it any better.
  3. That's a lesser-known loop construct in trigger and event is sort of lost in it I think.
  4. If you don't use semi-colons or braces the ENTIRE INTERNET NERDRAGES FOR TWO WEEKS.

I think your code is good though, those criticism are simply stylistic--though you're still at 20 lines due to all the "stuff" you have to write in JavaScript v. CoffeeScript.

The difference for me is that in CoffeeScript I can more easily say what I want to do and understand what I'm doing. For example, if I want a default argument its far easier to say it in the parameter list, and easier to understand that it takes a default argument later when I look at the function signature instead of having to notice some obscure context || this buried in the function body.

@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