First we set a flag -p
for the coffee
engine to print the JavaScript output instead of evaluating the code (engine.opts = '-p'
):
You need to install coffee
, the command-line version of CoffeeScript.
CoffeeScript compiles javascript:
@square = (x) -> x * x
// (function() {
// this.square = function(x) {
// return x * x;
// };
//
// }).call(this);
To use CoffeeScript in an HTML document, use results="asis"
, echo=FALSE
, and wrap the chunk in <script>
tags.
@square = (x) -> x * x
@cube = (x) -> square(x) * x
p = document.createElement("p");
p.appendChild(document.createTextNode("The cube of 3 is " + cube(3)))
document.body.appendChild(p)
Of course you can also run the code, if you remove the -p
flag from the chunk option engine.opts
(I'm not evaluating this code chunk here because I do not have a proper version of coffee
on Debian yet; if you do, you can remove eval=FALSE
):
x = 42
console.log "The answer is ", x