Coffeescript: using jQuery and underscore

I have been using coffeescript lately a lot to build a javascript library. The experience so far has been great as far as the complex programming I’m doing instead of using javascript. Coffeescript does the conversion for my code. I’m using jquery with coffeescript which is really easy to use nothing complex at all.

Here’s how I do it

$ ->
  $(some_link_id).click () ->
    # do something

which translates to

$(function() {
  return $(some_link_id).click(function() {
    // do something
  });
});

If you are going to use underscore you can just use as you would in your javascript functions i.e

_.map () ->

Yeah it’s that simple.

The coffeescript author has a coffee port of underscore which you can use directly if you are interested.

Mostly I have enjoyed using the loops in coffeescript since I’m more familiar with ruby syntax. It follows kind of the same way as I would do in ruby for ‘for loop’ or if using ‘when’

show = console.log # I like doing this to print in console
for code of codes
  show code

Coffeescript has great deal of resource in it’s website www.coffeescript.org. It’s really simple if you have ruby background and even if you don’t it’s just that simple to understand. I’m enjoying writing 300 lines of coffeescript instead of writing 1000 lines of javascript when coding OO style programming which transforms in javascript prototypical programming.

I hope you’ll like coffeescript as much I do. Enjoy!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: