*自分用メモ。随時更新
##文法
console.log
console.log 'aaa'
###変数とスコープ
myVariable = "test"
console.log myVariable
exports = this
exports.MyVariable = "foo-bar"
console.log exports
###関数
times = (a, b) -> a * b
console.log times(1, 2)
sum = (nums...) ->
result = 0
nums.forEach (n) -> result += n
result
console.log sum(1, 2, 3)
###オブジェクトと配列
object1 = {one: 1, twi: 2}
object2 = one: 1, two: 2
object3 =
one: 1
two: 2
console.log object1
console.log object2
console.log object3
array1 = [1, 2, 3]
array2 = [
1
2
3
]
array3 = [1,2,3,]
console.log array1
console.log array2
console.log array3
###条件分岐
if true == true
console.log "We're ok"
if true != true then console.log "Panic"
if 1 < 0 then console.log "Ok" else console.log "Y2K!"
###文字列の挿入
favorite_color = "Blue. no, yel.."
question = "Bridgekeeper: What... is your favorite color?
Galahd: #{favorite_color}
Bridgekeeper: Wrong!
"
console.log question
###ループ
for name in ["Roger the pickpocket", "Rodrick the robber"]
console.log "Release #{name}"
for name, i in ["Roger the pickpocket", "Rodrick the robber"]
console.log "#{i}.Release #{name}"
console.log prisoner for prisoner in ["Roger", "Roderick", "Brian"]
prisoners = ["Roger", "Roderick", "Brian"]
console.log prisoner for prisoner in prisoners when prisoner[0] is "R"
names = sam: 'seaborn', donna: 'moss'
console.log ("#{first} #{last}") for first, last of names
num = 6
minstrel = while num -= 1
console.log num + " Brave Sir Robin ran away"
###配列
range = [1..5]
console.log range
firstTwo = ["one", "two", "three"][0..1]
console.log firstTwo
numbers = [0..9]
numbers[3..5] = [-3, -4, -5]
console.log numbers
words = ["rattled", "roudy", "rebbles", "ranks"]
console.log "Stop wagging me" if "ranks" in words
##クラス
###インスタンスプロパティ
class Animal
price: 5
sell: "aaa"
animal = new Animal
console.log animal.sell
###静的プロパティ
class Animal
this.find = (name) ->
name
console.log Animal.find("Parrot")
###継承
class Animal
constructor: (@name) ->
alive: ->
false
class Parrot extends Animal
constructor: ->
super("Parrot")
dead: ->
not @alive()
dog = new Animal('Max')
parrot = new Parrot('Nic')
console.log dog.alive
console.log parrot.dead
class Animal
constructor: (@name) ->
class Parrot extends Animal
Animal::rip = true
parrot = new Parrot("Macaw")
console.log "This parrot is no more" if parrot.rip
##イディオム
###each
arr = [1,2,3,]
for val in arr then console.log val
obj =
king: 'King'
queen: 'Queen'
jack: 'Jack'
for key,val of obj
console.log val
###map
arr = [1,2,3,]
result = arr.map (i) -> i * 2
result = (item * 2 for item in arr)
console.log result
###select
arr = [1,2,3,4,5,6,]
result = (item for item in arr when item % 2 == 0)
console.log result
###includes
arr = [1,2,3,4,5,6,]
included = 2 in arr
console.log included
###property iteration
object = {one: 1, two: 2}
console.log ("#{key} = #{value}") for key, value of object
###Min/Max
max = (Math.max [1,2,3,4,5,]...)
min = (Math.min [1,2,3,4,5,]...)
console.log max
console.log min
###destructuring assignment
someObject = {a: 'value for a', b: 'value for b'}
{a, b} = someObject
console.log "a is '#{a}', b is '#{b}'"