Javascript/nodejs/coffee-script
提供: 初心者エンジニアの簡易メモ
2015年5月20日 (水) 03:18時点における127.0.0.1 (トーク)による版 (ページの作成:「==インストール== # npm install -g coffee-script ==バージョン確認== $ coffee -v ==helloworld== *hello.coffee hello = -> console.log "Hello World!"...」)
目次
インストール
# npm install -g coffee-script
バージョン確認
$ coffee -v
helloworld
- hello.coffee
hello = ->
console.log "Hello World!"
hello()
- コンパイル
$ coffee -c hello.coffee
- hello.jsが出力される
(function() {
var hello;
hello = function() {
return console.log("Hello World!");
};
hello();
}).call(this);
配列
arr=[1,2,3]
オブジェクト
object= name: "hoge" age : 19
ファンクション
- coffee
square = (x) -> x * x console.log square(4) // 4
- js
square = function(x) {
return x * x;
};
for文
ary = [
"abc"
"def"
"ghi"
"jkl"
]
for value in ary
console.log value
if文
value = 10
if value is 10
console.log "ok"
else
console.log "ng"
変数展開
name = "hoge1"
console.log "hoge #{name}" // hoge hoge1
this変数
test = -> name = "hoge1" console.log @name
クラス文
class Animal
constructor: (@name) ->
foot: (count) ->
@name + " footcount " + count
class Cat extends Animal
foot: ->
super 4
cat = new Cat "cat"
console.log cat.foot()
try catch
try throw "throw error" catch error console.log error finally console.log error
ヒアドキュメント
heredoc = HERE Document
イベントバインド
Account = (customer, cart) ->
@customer = customer
@cart = cart
$('.shopping_cart').bind 'click', (event) =>
@customer.purchase @cart
参考
http://tech.kayac.com/archive/coffeescript-tutorial.html
