Hacker News new | ask | show | jobs
by hermanthegerman 5577 days ago
I have to say that by using cs I learned a lot more about js. Maybe for old js cats it doesnt matter, but I only used js occasionally before, and now it became one of my favorite languages (I use with node, and I write all my node stuff in cs).

One particular example that changed everything for me was the natural and simple way I could build the typical callback constructs with -> and => , so they almost look like blocks in ruby, and feel way more natural as a control construct, and I didnt have to stack lots of })}); and so on.

If you're rather new to something, the ease of use and the simplicity and readability speeden up the learning process extremely.

That being said, there are also a couple of things in cs that are kind of silly.. like using 'for i in my_array', but 'for k of my_dict', and stuff like that, that are really hard to debug.

Although, in terms of debugging, the fact that it gets compiled and possible errors are early detected and a canonical js is generated also saved me lot of time, in particular with IE ;-)

2 comments

To explain the reason for the "in/of" distinction...

"for item in list" vs "for key, value of object" is an unfortunate necessary evil. It would be great to use the same keyword, "in", for both types of loop, but I'm afraid there's no way for us to know at compile time if "list" or "object" is really an array, or really an object.

The fact that cs is doing this 'the right way', meaning safe for all environments, as you have explained below, is one of the reasons I like it.
I'm curious, why do you need to make a distinction? Is there some other feature of CS that depends on it? Certainly 'for key, value of array' could be implemented with numerical keys. If using just 'for value of dict' is allowed that would also work fine.
We need to make a distinction because we want to have arrays be iterated over with:

    for (var i = 0, l = list.length; i < l; i++) { ... }
And objects iterated over with:

    for (var key in object) { ... }
Using a "for-in" loop over a JS array isn't acceptable, for performance and semantics reasons, and neither is sniffing at runtime to determine whether the object passed is an array, or an object.

Ideally, JavaScript would have supported a single iteration protocol for both arrays and objects from the get-go, but alas...

I'm new to programming and learned a little bit of javascript lately. I'm going to do a mini project on Node just to give myself an excuse to learn more programming.

If you dont mind my asking, how would i go about using CoffeeScript with Node?

If you build those simple 1-file-testservers with node, simply write them in coffee and then start them like "coffee myserver.coffee".

If it's more elaborated, you might want to set up some kind of build process and then use the generated js files. You could use a simple Makefile and compile your coffee files with coffee -c -b.

Oh, i forgot: You can add the --watch switch and get your scripts compiled on save.
I'll try that. Thanks!