CoffeeScript is a little language that compiles into javaScript. Underneath that awkward Java-esque patina, Javascript has always had a gorgeous heart. CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way.
The golden rule of CoffeeScript is: "It's just JavaScript". The code compiles one-to-one into the equivalent JS, and there is no interPRetation at runtime. You can use any existing JavaScript library seamlessly from CoffeeScript (and vice-versa). The compiled output is readable and pretty-printed, will work in every JavaScript runtime, and tends to run as fast or faster than the equivalent handwritten JavaScript.
Latest Version: 1.8.0
npm install -g coffee-script
CoffeeScript on the left, compiled JavaScript output on the right.
# Assignment:number = 42opposite = true# Conditions:number = -42 if opposite# Functions:square = (x) -> x * x# Arrays:list = [1, 2, 3, 4, 5]# Objects:math = root: Math.sqrt square: square cube: (x) -> x * square x# Splats:race = (winner, runners...) -> print winner, runners# Existence:alert "I knew it!" if elvis?# Array comprehensions:cubes = (math.cube num for num in list)
var cubes, list, math, num, number, opposite, race, square, __slice = [].slice;number = 42;opposite = true;if (opposite) { number = -42;}square = function(x) { return x * x;};list = [1, 2, 3, 4, 5];math = { root: Math.sqrt, square: square, cube: function(x) { return x * square(x); }};race = function() { var runners, winner; winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(arguments, 1) : []; return print(winner, runners);};if (typeof elvis !== "undefined" && elvis !== null) { alert("I knew it!");}cubes = (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = list.length; _i < _len; _i++) { num = list[_i]; _results.push(math.cube(num)); } return _results;})();
run: cubesThe CoffeeScript compiler is itself written in CoffeeScript, using the Jison parser generator. The command-line version of coffee is available as a Node.js utility. The core compiler however, does not depend on Node, and can be run in any JavaScript environment, or in the browser (see "Try CoffeeScript", above).
To install, first make sure you have a working copy of the latest stable version of Node.js, and npm (the Node Package Manager). You can then install CoffeeScript globally with npm:
npm install -g coffee-script
When you need CoffeeScript as a dependency, install it locally:
npm install --save coffee-script
If you'd prefer to install the latest master version of CoffeeScript, you can clone the CoffeeScript source repository from GitHub, or download the source directly. To install the latest master CoffeeScript compiler with npm:
npm install -g jashkenas/coffeescript
Or, if you want to install to /usr/local, and don't want to use npm to manage it, open the coffee-script directory and run:
sudo bin/cake install
Once installed, you should have access to the coffee command, which can execute scripts, compile .coffee files into .js, and provide an interactive REPL. The coffee command takes the following options:
-c, --compile | Compile a .coffee script into a .js JavaScript file of the same name. |
-m, --map | Generate source maps alongside the compiled JavaScript files. Adds sourceMappingURL directives to the JavaScript as well. |
-i, --interactive | Launch an interactive CoffeeScript session to try short snippets. Identical to calling coffee with no arguments. |
-o, --output [DIR] | Write out all compiled JavaScript files into the specified directory. Use in conjunction with --compile or --watch. |
-j, --join [FILE] | Before compiling, concatenate all scripts together in the order they were passed, and write them into the specified file. Useful for building large projects. |
-w, --watch | Watch files for changes, rerunning the specified command when any file is updated. |
-p, --print | Instead of writing out the JavaScript as a file, print it directly to stdout. |
-s, --stdio | Pipe in CoffeeScript to STDIN and get back JavaScript over STDOUT. Good for use with processes written in other languages. An example: cat src/cake.coffee | coffee -sc |
-l, --literate | Parses the code as Literate CoffeeScript. You only need to specify this when passing in code directly over stdio, or using some sort of extension-less file name. |
-e, --eval | Compile and print a little snippet of CoffeeScript directly from the command line. For example:coffee -e "console.log num for num in [10..1]" |
-b, --bare | Compile the JavaScript without the top-level function safety wrapper. |
-t, --tokens | Instead of parsing the CoffeeScript, just lex it, and print out the token stream: [IDENTIFIER square] [ASSIGN =] [PARAM_START (] ... |
-n, --nodes | Instead of compiling the CoffeeScript, just lex and parse it, and print out the parse tree:Expressions Assign Value "square" Code "x" Op * Value "x" Value "x" |
--nodejs | The node executable has some useful options you can set, such as --debug, --debug-brk, --max-stack-size, and --expose-gc. Use this flag to forward options directly to Node.js. To pass multiple flags, use --nodejs multiple times. |
Examples:
Besides being used as an ordinary programming language, CoffeeScript may also be written in "literate" mode. If you name your file with a .litcoffee extension, you can write it as a Markdown document — a document that also happens to be executable CoffeeScript code. The compiler will treat any indented blocks (Markdown's way of indicating source code) as code, and ignore the rest as comments.
Just for kicks, a little bit of the compiler is currently implemented in this fashion: See it as a document, raw, and properly highlighted in a text edit
新闻热点
疑难解答