CoffeeScript学习笔记

1、为何使用CoffeeScritp
google主导了javasctipt的创新,node更是引领了这一波强势回归,但是总体来说javascript的语法过于自由与繁琐,相对python、ruby等现代语言,自然性、易读性都有所欠缺(毕竟javascript是一种老版本语言呀,呵呵),而CoffeeScript恰恰解决了这个问题
2、如何安装
这比较简单(我的是ubuntu 12.04),首先需要安装node,这个不再这里详细说明,然后直接使用npm -g install coffeesctipt即可,或者直接使用sudo apt-get install coffeesctipt也可以安装哦
使用coffee -v可以查看安装的版本
3、主要的语法说明
3.1、function
name = (param,param2=value)->
body
主要使用->来区分函数体,同时需要使用缩进来表示函数体范围(雷同python)
3.2、coffee自动判断不同使用范围你的变量作用域问题,保证变量安全性问题;
3.3、条件语句
express if condition
if condisiotn
pass
else
pass
a = if condition then b else c
3.4、函数对不定参数的支持
==========例子代码=============
gold = silver = rest = "unknown"
awardMedals = (first, second, others...) ->
  gold   = first
  silver = second
  rest   = others
contenders = [
  "Michael Phelps"
  "Liu Xiang"
  "Yao Ming"
  "Allyson Felix"
  "Shawn Johnson"
  "Roman Sebrle"
  "Guo Jingjing"
  "Tyson Gay"
  "Asafa Powell"
  "Usain Bolt"
]
awardMedals contenders...
alert "Gold: " + gold
alert "Silver: " + silver
alert "The Field: " + rest
3.5、循环操作
--------代码例子------------
# Eat lunch.
eat food for food in ['toast', 'cheese', 'wine']
# Fine five course dining.
courses = ['greens', 'caviar', 'truffles', 'roast', 'cake']
menu i + 1, dish for dish, i in courses
# Health conscious meal.
foods = ['broccoli', 'spinach', 'chocolate']
eat food for food in foods when food isnt 'chocolate'
countdown = (num for num in [10..1])  # 生成数据
yearsOld = max: 10, ida: 9, tim: 11
ages = for child, age of yearsOld
  "#{child} is #{age}"
# Econ 101
if this.studyingEconomics
  buy()  while supply > demand
  sell() until supply > demand
# Nursery Rhyme
num = 6
lyrics = while num -= 1
  "#{num} little monkeys, jumping on the bed.
    One fell out and bumped his head."
for filename in list
  do (filename) ->
    fs.readFile filename, (err, contents) ->
      compile filename, contents.toString()
3.6、数组切片
middle  = numbers[3...6] # 不包含6
middle  = numbers[3..6] # 包含6
同样效果可以定义数组
arr = [0..10]
arr = [0...10]
3.7、coffee中的特点是所有地方都是表达式,也就是说都有值(空的话就是null)
3.8、操作符问题
CoffeeScript JavaScript
is ===
isnt !==
not !
and &&
or ||
true, yes, on true
false, no, off false
@, this this
of in
in no JS equivalent
还有扩展的操作符号
solipsism = true if mind? and not world?
speed = 0
speed ?= 15
footprints = yeti ? "bear"
3.9、类定义
------------代码------------
class Animal
  constructor: (@name) ->
  move: (meters) ->
    alert @name + " moved #{meters}m."
class Snake extends Animal
  move: ->
    alert "Slithering..."
    super 5
class Horse extends Animal
  move: ->
    alert "Galloping..."
    super 45
sam = new Snake "Sammy the Python"
tom = new Horse "Tommy the Palomino"
sam.move()
tom.move()
3.10、swtich
----------代码---------------
switch day
  when "Mon" then go work
  when "Tue" then go relax
  when "Thu" then go iceFishing
  when "Fri", "Sat"
    if day is bingoDay
      go bingo
      go dancing
  when "Sun" then go church
  else go work
3.11、try。。。catch。。。
==========代码-----------
try
  allHellBreaksLoose()
  catsAndDogsLivingTogether()
catch error
  print error
finally
  cleanUp()
3.12、其他特别的
三连判断,比较自然,呵呵
healthy = 200 > cholesterol > 60
字符串中显示内容:
author = "Wittgenstein"
quote  = "A picture is a fact. -- #{ author }"
sentence = "#{ 22 / 7 } is a decent approximation of π"
三引号多行字符串
tml = """
       <strong>
         cup of coffeescript
       </strong>
       """
同样三井号多行注释
###
CoffeeScript Compiler v1.3.3
Released under the MIT License
###
posted @ 2012-06-21 11:27  orlla  阅读(316)  评论(0编辑  收藏  举报