ziz

Shadowing variables in CoffeeScript

Jun 12, 2012 15:35


CoffeeScript is a meta-language which compiles directly into JavaScript, has some neat features and patterns baked in, and is a favorite of many JavaScript hackers, and I’ve been playing around with it in the last few days.

For the most part, I like it, but it steps on one of the most useful features of JavaScript - the var keyword for scope ( Read more... )

coffeescript, tech, javascript, scope

Leave a comment

Comments 5

okterok June 13 2012, 00:32:59 UTC
I read "bat ticks" about 3 times before I got that you said "back ticks" >.< really need more sleep.

So why would a person use Coffee Script over JavaScript?

Reply

ziz June 13 2012, 14:07:39 UTC
Well, it has a lot of nice features that javascript doesn't make easy, like list comprehensions, existence tests, conditional assignment, and so forth. My favorite feature is the much shorter syntax for function binding, which is extremely helpful when you're writing jquery. From this tumblr, for example -

CoffeeScript:

object =
func: -> $('#div').click => @element.css color: 'red'

compiles to JavaScript:

var object;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
object = {
func: function() {
return $('#div').click(__bind(function() {
return this.element.css({
color: 'red'
});
}, this));
}
};

Shorter and quicker to write, easier to scan, with much less boilerplate for doing the same constantly-repeated patterns in javascript.

Reply

okterok June 13 2012, 15:08:29 UTC
Wow that's really slick! Do you have a tutorial url to share?

Reply

ziz June 13 2012, 15:16:14 UTC
Sure - the reference is at coffeescript.org (and you can try it out online there, too), and another take on introducing the features is at tutsplus. A bunch of tutorials are linked at this blog, and on quick glance, it looks like there's some worthwhile ones there.

Reply


Leave a comment

Up