すずけんメモ

技術メモです

なんとなく思いついたのでCoffeeScriptで遊んでみることにします。

 

CoffeeScript

http://coffeescript.org/

 

まずはインストール。npmを利用。

 

$ npm install -g coffee-script

 

/usr/local/bin/coffee -> /usr/local/lib/node_modules/coffee-script/bin/coffee

/usr/local/bin/cake -> /usr/local/lib/node_modules/coffee-script/bin/cake

coffee-script@1.2.0 /usr/local/lib/node_modules/coffee-script

 

入った。

 

$ coffee -h

 

Usage: coffee [options] path/to/script.coffee

 

If called without options, `coffee` will run your script.

 

  -b, --bare         compile without a top-level function wrapper

  -c, --compile      compile to JavaScript and save as .js files

  -e, --eval         pass a string from the command line as input

  -h, --help         display this help message

  -i, --interactive  run an interactive CoffeeScript REPL

  -j, --join         concatenate the source CoffeeScript before compiling

  -l, --lint         pipe the compiled JavaScript through JavaScript Lint

  -n, --nodes        print out the parse tree that the parser produces

      --nodejs       pass options directly to the "node" binary

  -o, --output       set the output directory for compiled JavaScript

  -p, --print        print out the compiled JavaScript

  -r, --require      require a library before executing your script

  -s, --stdio        listen for and compile scripts over stdio

  -t, --tokens       print out the tokens that the lexer/rewriter produce

  -v, --version      display the version number

  -w, --watch        watch scripts for changes and rerun commands

 

 

なるほど。

 

Exampleを見ると、

 

square = (x) -> x * x

 

 

function square(x){

     return x * x;

}

 

となっている。

 

配列は

 

test = [1, 2, 3, 4, 5]

 

で、

 

var test = [1, 2, 3, 4, 5];

 

となり、オブジェクトは、

 

test = {hoge: "boo", fuga: "bar"}

 

で、

 

var test = {hoge: "boo", fuga: "bar"};

 

となる。セミコロンいらないのか。試してみる。

 

--console--

 

$ cat test.coffee

test = [1,2,3,4,5]

$ coffee -c test.coffee

$ ls

test.coffee test.js

$ cat test.js

(function() {

  var test;

 

  test = [1, 2, 3, 4, 5];

 

}).call(this);

 

ちゃんと変換された。(function(){}).call(this); になってるのは、すぐに呼び出すからか。

 

また、

 

kids =

  brother:

    name: "Max"

    age:  11

  sister:

    name: "Ida"

    age:  9

 

 

で、

 

kids = {

  brother: {

    name: "Max",

    age: 11

  },

  sister: {

    name: "Ida",

    age: 9

  }

};

 

となる。なるほど。実際にやってみる。

 

$ coffee -c indent.coffee

$ ls

indent.coffee indent.js     test.coffee   test.js

$ cat indent.js

(function() {

  var kids;

 

  kids = {

    brother: {

      name: "Max",

      age: 11

    },

    sister: {

      name: "Ida",

      age: 9

    }

  };

 

}).call(this);

 

できた。

 

JavaScriptで、

 

var changeNumbers, inner, outer;

 

outer = 1;

 

changeNumbers = function() {

  var inner;

  inner = -1;

  return outer = 10;

};

 

inner = changeNumbers();

 

と書いてスコープをよしなに扱っていたものが、Coffeeでは

 

outer = 1

changeNumbers = ->

  inner = -1

  outer = 10

inner = changeNumbers()

 

で良いらしい。楽だ。