Skip to content

Instantly share code, notes, and snippets.

# for
# スタンダードな使い方。
sum = 0
for i in [0..9]
sum += i
console.log sum
# 処理文と条件文の順序を逆にすることもできる
sum = 0
// Generated by CoffeeScript 1.9.3
(function() {
var i, j, k, l, sum, total;
sum = 0;
for (i = j = 0; j <= 9; i = ++j) {
sum += i;
}
# array
for color in ["red", "blue", "pink"]
console.log color
# 1行で書くとこんな感じ
console.log color for color in ["red", "blue", "pink"]
# whenで条件指定
console.log color for color in ["red", "blue",
"pink"] when color isnt "blue"
// Generated by CoffeeScript 1.9.3
(function() {
var color, i, j, k, l, len, len1, len2, len3, m, name, ref, ref1, ref2, ref3, result, score;
ref = ["red", "blue", "pink"];
for (j = 0, len = ref.length; j < len; j++) {
color = ref[j];
console.log(color);
}
# function
###
JSではこう書く
function hello() {}
var hello = function() {}
coffeeでは後者!
###
# 基本形
// Generated by CoffeeScript 1.9.3
/*
JSではこう書く
function hello() {}
var hello = function() {}
coffeeでは後者!
*/
(function() {
# 分割代入
[a, b, c] = [1, 5, 10]
console.log a for a in [a, b, c]
# 1 5 10
# 入れ替え
x = 10
y = 20
show = (a, b) -> console.log "#{a} : #{b}"
show(x, y) # 10 : 20
// Generated by CoffeeScript 1.9.3
(function() {
var a, b, c, email, i, j, len, len1, name, ref, ref1, ref2, ref3, ref4, results, show, user, x, y;
ref = [1, 5, 10], a = ref[0], b = ref[1], c = ref[2];
ref1 = [a, b, c];
for (i = 0, len = ref1.length; i < len; i++) {
a = ref1[i];
console.log(a);
# class
log = (x) -> console.log x
# 大文字から始まる
class User
constructor : (name) ->
this.name = name
# thisは@で置き換えてもOK
// Generated by CoffeeScript 1.9.3
(function() {
var User, log, me, you;
log = function(x) {
return console.log(x);
};
User = (function() {
function User(name) {