
1、为何使用CoffeeScritpgoogle主导了javasctipt的创新,node更是引领了这一波强势回归,但是总体来说javascript的语法过于自由与繁琐,相对python、ruby等现代语言,自然性、易读性都有所欠缺(毕竟javascript是一种老版本语言呀,呵呵),而CoffeeScript恰恰解决了这个问题2、如何安装这比较简单(我的是ubuntu 12.04),首先需要安装node,这个不再这里详细说明,然后直接使用npm -ginstall coffeesctipt即可,或者直接使用sudo apt-get installcoffeesctipt也可以安装哦使用coffee -v可以查看安装的版本3、主要的语法说明3.1、functionname = (param,param2=value)->body主要使用->来区分函数体,同时需要使用缩进来表示函数体范围(雷同python)3.2、coffee自动判断不同使用范围你的变量作用域问题,保证变量安全性问题;3.3、条件语句express if conditionif condisiotnpasselsepassa = if condition then b else c3.4、函数对不定参数的支持==========例子代码=============gold = silver = rest = "unknown"awardMedals = (first, second, others...)-> gold = first silver = second rest = otherscontenders = [ "Michael Phelps" "Liu Xiang" "Yao Ming" "Allyson Felix" "Shawn Johnson" "Roman Sebrle" "Guo Jingjing" "Tyson Gay" "Asafa Powell" "Usain Bolt"]awardMedals contenders...alert "Gold: " + goldalert "Silver: " + silveralert "The Field: " + rest3.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: 11ages = for child, age of yearsOld "#{child} is #{age}"# Econ 101if this.studyingEconomics buy() while supply> demand sell() until supply >demand# Nursery Rhymenum = 6lyrics = while num -= 1 "#{num} little monkeys, jumping on thebed. One fell out and bumpedhis head."for filename in list do (filename) -> fs.readFile filename,(err, contents) -> compile filename, contents.toString()3.6、数组切片middle = numbers[3...6] # 不包含6middle = numbers[3..6] # 包含6同样效果可以定义数组arr = [0..10]arr = [0...10]3.7、coffee中的特点是所有地方都是表达式,也就是说都有值(空的话就是null)3.8、操作符问题CoffeeScript JavaScriptis ===isnt !==not !and &&or ||true, yes, on truefalse, no, off false@, this thisof inin no JS equivalent还有扩展的操作符号solipsism = true if mind? and not world?speed = 0speed ?= 15footprints = yeti ? "bear"3.9、类定义------------代码------------class Animal constructor: (@name)-> move: (meters) -> alert @name + " moved#{meters}m."class Snake extends Animal move: -> alert"Slithering..." super 5class Horse extends Animal move: -> alert"Galloping..." super 45sam = 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 gobingo godancing when "Sun" then go church else go work3.11、try。。。catch。。。==========代码-----------try allHellBreaksLoose() catsAndDogsLivingTogether()catch error print errorfinally 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.3Released under the MIT License###