Friday, August 17, 2012

Coffeescript to javascript bare

I learned something new today.

I noticed that when I run coffee -p, the javascript printout on my console is wrapped in a function.

Let me give you an example. Create a coffeescript (name it, say, with the following...
console.log 'hello world'
Now, if I run coffee -p, I get...
(function() {

  console.log('hello world');

At this time, I am not sure if I can use that script as is when I insert it in my sinatra application. Fortunately, I found that if I run the file using

coffee -pb

I would get a more one to one translation of coffeescript into javascript output

console.log('hello world');

The "-pb" flag means, -print, and -bare. This tells coffee to give me the javascript equivalent of the .coffee file, and to not wrap the js output with a function.

I got this idea from the book, CoffeeScript: Accelerated JavaScript Development by Trevor Burnham.

Did I get that right? If not, feel free to correct me in the comment section below. Thanks!

No comments:

Post a Comment