coffeeScript demo

#可以直接在瀏覽器嵌入coffee-script.js 解析xx.coffee腳本,但真正正式使用時不建議這樣使用,coffee-script.js下載地址:http://coffeescript.org/extras/coffee-script.js
#本地安裝coffee 可以把寫好的xx.coffee 編譯成 xx.js  命令:coffee -c xx.coffee
#變量
myVar = 1
#數組
myArray = [1, 2, 3, 4, 5]
#對象
leader =
  name: "wj"
  age: 25
alert(leader.name)
#函數 if x == null x default 6
square = (x = 6) ->
  x * x
alert(square(myVar))
alert(square())

#判斷
#三目運算
myVar = 0 if leader.name == 'wj'
alert(myVar)

if myVar == 0
  myVar = 1
else if myVar == 1
  myVar = 2
else
  myVar = 3


#遍歷
myVar = myVar + x for x in myArray;

myVar = myVar+x for x in myArray when x == 5

alert(myVar)

發佈了45 篇原創文章 · 獲贊 20 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章