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) | |
} | |
} |
- 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 ;) - 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.
- Experienced JavaScript programmers know and use that construct.There's nothing unusual about that.
- 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
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.trigger
andevent
is sort of lost in it I think.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.