首页 > 代码库 > coffeeScript demo

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)

coffeeScript demo