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)

posted @ 2016-04-05 19:59  phlsheji  阅读(178)  评论(0编辑  收藏  举报