Skip to content

Instantly share code, notes, and snippets.

@shinsuke79
Created November 29, 2012 17:17
Show Gist options
  • Save shinsuke79/e0def5d5776726abd573 to your computer and use it in GitHub Desktop.
Save shinsuke79/e0def5d5776726abd573 to your computer and use it in GitHub Desktop.
CoffeeScriptのまとめ
###
coffeeによるオブジェクト指向は、jsと違ってオブジェクト指向に近い構成になっている。
###
# 親クラス
class People
# インスタンス変数
name: undefined
age : undefined
# インスタンスメソッド
toString: () ->
this.name + this.age
# クラス変数
@kind: "人間"
# クラスメソッド
@getSex: () ->
["man","women"]
# コンストラクタ
constructor: (name, age) ->
this.name = name
this.age = age
# ホントは@nameでもいけるけど俺はパス.ややこしい
# スタティック呼び出す場合はあくまでもクラス名.とかにしたほうがいいと思う
# 継承クラス
class Student extends People
# インスタンス変数
id: undefined
# コンストラクタ
constructor: (name, age, id) ->
super(name, age)
this.id = id
people = new People("yone",20)
student = new Student("たいぞう",2,1111)
# ========== グローバル関数 ========== #
parseInt("111") # intに変換.第二引数は基数
parseFloat("12.0") # floatに変換
isNaN(10/0) # 値がNaNかチェック
isFinite(Infinity) # 有限値ならtrue. -Infinityってのもある
Date() # 現在の日付の文字列
# ========== Date ========== #
new Date() # 現在の日付と時刻
new Date(ms) # 引数一つならmsで判断
new Date(str) # 文字列一つならアナライズ
new Date(year, month[, day, hour, min, sec, ms]) # 2つ以上ならそれぞれ指定
# yearはそのまま、月は0~11, 日は1~31, 時は0~23, 分秒は0~59, ミリ秒は0~999
date.toString() # すべて文字列化
date.toDateString() # 日付のみ文字列化
date.toTimeString() # 時刻のみ文字列化
# toLocaleString / toLocaleDateString / toLocaleTimeStringはローカライズされた文字列
date.toUTCString() # その時刻でのUTC時間を返す
date.valueOf() # ミリ秒に変換
Date.parse("2009/08/11 08:14:45") # 文字列からDateオブジェクト生成
# 各時間を取るのはこうするらしい(setterも同じようにあるよ)
# getFullYear / getMonth / getDate / getDay
# getHours / getMinutes / getSeconds / getMilliseconds
# ========== Math ========== #
Math.abs(val) # 絶対値
Math.round(val) # 四捨五入
Math.ceil(val) # 切り上げ
Math.floor(val) # 切り捨て
Math.max(val1, val2) # 最大値を返す.可変長引数.minもあり
Math.random() # ランダムな値を返す
Math.sin(rad) # sin,cos,tan,asin,acos,atan(ラジアン)
Math.sqrt(val) # 平方根
Math.pow(x, n) # xのn乗
Math.log(val) # log e valを返す
Math.exp(val) # eのval乗
# ========== String ========== #
s.length() # 文字列長(全角半角関係なし)
s.concat(val1, val2) # 可変長引数。つなげた文字列を返す
s.slice(0, 1) # スライサー(substring, substrも一緒?)
s.charAt(0) # 指定された番号の文字
s.charCodeAt(0) # 指定された番号の文字コードを返す
String.fromCharCode(12497, 12477) # 可変長引数。文字コードから文字列生成
s.toLowerCase() # すべて小文字(toUpperCaseもあるよ)
s.indexOf("文字列",n) # n番目以降で指定された文字列が存在する番号を返す.lastindexもあるよ
# ========== 特殊 ========== #
# 多重代入
a = 1
b = 2
c = 3
[a,b,c] = [2,4,6]
[a,b...,c] = [a, "xxx".split("")] # こんなのもできる
# 範囲オブジェクト
[0..5] # 0~5まで
# 文字列の式展開
"文字列#{variable}だよ"
# ヒアドキュメント
'''
この間がヒアドキュメントだよ
インデントしても大丈夫だよ
'''
# thisのバインド
# -> の代わりに => を使うと外側のthis.を使えるようになる
# ========== for文 ========== #
# for文 #
for i in [0..9]
# 処理 & break & continue
for key of obj
# 処理 & break & continue
for value in ary
# 処理 & break & continue
for key, value of obj
# 処理 & break & continue
for value, index in ary
# 処理 & break & continue
# ========== while文 ========== #
while "条件"
# 処理 & break & continue
"処理" while "条件"
until "条件の逆"
# 処理 & break & continue
"処理" until "条件の逆"
# ========== if文 ========== #
if "条件"
# 処理
else if "条件"
# 処理
else
# 処理
"処理" if "条件"
# 条件にはif obj?も使える。この場合nullとundefined以外はtrueを返す
# rubyにあるunlessはif not 条件 で適用可能
# 連結条件式も使えるらしい。
if 0 < x < 10
# 処理
# 演算子まとめ
enzanshi =
is : "==="
isnt : "!=="
not : "!"
and : "&&"
or : "||"
true : "true" , yes : "true" , on : "true"
false: "false", no : "false", off: "false"
@ : "this" , this: "this"
of: "in"
in: "無し"
# ========== switch文 ========== #
switch "値"
when "値1" then "1に対する処理処理"
when "値2", "値3" then "2,3に対する処理"
when "値4"
"4に対する処理"
else
"その他の処理"
# ========== 配列 ========== #
# これは普通
ary = [1,2,3];
ary[2..4] # 配列番号2~4
ary[2...3] # 配列番号2~3
# 代入も可能
# 多次元配列
ary = [
[1,2,3]
[4,5,6]
[7,8,9]
]
ary.length; # 長さ
ary.toString # 文字列化
ary.join(":") # 指定された文字列で連結した文字列
ary.sort((var1,var2) -> "式") # 引数なしもOK
# ========== オブジェクト ========== #
taizo =
name: "たいぞう"
hello: ()->
console.log "hello! I am #{name}!!!"
obj.hello # こう使えばオブジェクトだし
obj["hello"] # こう使えばハッシュ
@shinsuke79
Copy link
Author

2つ目のサイトに書いてある「全ては式」ってのはCoffeeScriptにおいて大事な要素っぽいから今度読んどこうかな

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment