Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to bare option when using Coffeescript.compile

Tags:

coffeescript

I am using coffee-script.js to let me compile little snippets of coffeescript into javascript inside the browser. This lets me do:

eval(Coffeescript.compile("console.log 'yo'"))

But this returns a wrapped function, like this:

(function() {

  console.log('yo')

}).call(this);

I would like to get an unwrapped snippet of code, so that I can call functions that would be in scope if it wasn't for the function wrapper. Any suggestions apart from a regex to strip it out by hand?

like image 690
Mike McKay Avatar asked Sep 14 '12 02:09

Mike McKay


1 Answers

You can pass the bare option in the second argument of compile:

// In JS
CoffeeScript.compile("console.log 'yo'", {bare: true})

# In CS :)
CoffeeScript.compile "console.log 'yo'", bare: on

But you could also use CoffeeScript.eval directly. It will do just what you're looking for :)

like image 142
epidemian Avatar answered Sep 27 '22 18:09

epidemian