CoffeeScript: Write once, run anywhere?
Why I write `var`
I like CoffeeScript. It's a great little language. I like it so much that I never want to write regular JavaScript. But I do write regular JavaScript.
Why?
Because sometimes I don't have CoffeeScript available to me. I could. npm install coffee-script
is quick and easy. But sometimes I have a JavaScript execution environment and I don't have CoffeeScript.
I want a way to have it everywhere I have JavaScript, without having to think about it. I don't want to have to know if I should grab it from a cdn or install it via npm or fetch it via browserify. Or however I should do it with heroku. If I have a textarea where I can type JavaScript, I want to be able to just paste CoffeeScript instead and have it run.
So that's my goal. I figure the weakest and most portable and least coupled JavaScript execution environment you could have is an eval
.
Here's where I'm starting:
var result = eval('\
var a=0.5;\
var b=0.5;\
var c=a * b;\
return c;
);
And here's where I want to be:
var coffeescript = '\
a = 0.5
b = 0.5
c = a * b';
var begin-magic = // ...
var end-magic = // ...
var result = eval(begin-magic + coffee-script + end-magic);
And then I want to end up with the same result.
If I can do this, then I can write CoffeeScript and never have to go back to writing var
because I'm in some environment where I don't have it.
I haven't figured out how to do this yet. So I filed a StackOverflow question about it.
Manhattan mobile software engineer.