CoffeeScript中有一个很是有用的存在运算符?
,它能正确地处理值是否存在(存在的意思为变量不为undefined或者null)的状况。在变量后添加?
来判断它是否存在。数组
注意,若是?运算符后没有参数,那么在使用?运算符时必须紧靠在标识符后,不能有空格,不然会按照函数调用编译,编译出错。app
if yeti? 'I want to believe' ### 上面的代码被编译为: if(typeof yeti != null){ 'I want to believe' } ###
经过在链式调用中使用?
能够防止空值形成类型错误,同时,这样作也没法对不一样层次的属性的不存在分别处理。ide
tree = pine: type: 'evergreen' crabapple: type: 'deciduous' fruit: ediable: false if trees.pine.fruit?.edible console.log "Mmm.. pine fruit"
对于数组和函数调用,也可以使用?
运算符。函数
# 数组 alpha = lowercase: ['a','b','c','d'] console.log alpha.lowercase?[2].toUpperCase() # 执行 console.log alpha.uppercase?[2].toLowerCase() # 不执行 # 函数 oppositeMath = min: Math.max console.log oppositeMath.min?(3.2.5) # 执行 console.log oppositeMath.max?(3,2,5) # 不执行
brief = null breif ?= 2 # breif为2
上面的例子中须要注意的是:?=
运算符左侧不能使用未声明的变量,不然编译会出错,由于变量不是有效引用。?
运算符不受此约束。ui
||
和比较运算符都被扩展了,也就是说||=
和or=
都是合法的。code
CoffeeScript提供了解构赋值的功能,即只用一个表达式,就能够实现给数组或者对象中的多个变量赋值。对象
[first, second] = ["home","cart"] [first, second] = [second, first] # 变量替换 [drink,[alcohol, mixer]] = ["Screwdriver", ["vodka", "orange juice"]] #
同时,咱们能够在使用返回数组(多个值)的函数时使用。coffeescript
[languageName, prefix] = "CoffeeScript".match /(.*)Script/ console.log "I love the smell of #{prefix} in the morning."
同时,解构赋值还能够用于对象,将变量付给特定名字,具体有两种用法:ip
{property} = object
,其中,property为对象中属性的名字,复制后成为单独的变量能够直接使用{property: identifier} = object
, 其中,property为对象中属性的名字,identifier为给定的变量名(能够事先不声明)。赋值以后identifier的值变成对象中property对应的值。bird = verb: 'singing', time: 'midnight' {time} = bird # time为'midnight' {time:timeOfDate} = bird # timeOfDate为'midnight'
同时,能够互相嵌套对象,还能够在对象中嵌套数组。ci
# 使用对象嵌套赋值 direction = [ {type: 'boat', directions: ['port', 'starboard']} {type: 'dogsled', directions: ['haw', 'gee']} ] [boatInfo, {directions: [left, right]}] = direction console.log 'boatInfo: ' + boatInfo.type # 输出 boatInfo: boat console.log 'left: ' + left + '; right: ' + right # 输出 left: haw; right: gee
CoffeeScript中的函数支持默认函数参数。
func = (a, b = 'b') -> console.log a + ',' + b func 'a' # 输出 a,b func 'a', null # 输出 a,b func 'a', 'c' # 输出 a,c
在函数定义的参数后添加...
,即将此参数声明为可接受任意数目参数的变量。
func2 = (a,b...)-> console.log b.length func2 1,2,3,4 # 输出 3
可变参数不必定要在参数列表最后一个。同时,解构赋值也可使用可变参数。
[race, [splits..., time]] = ['10K',['13:08','13:09','23:17']] console.log race # 输出 10K console.log splits # 输出 ['13:08','13:09']
不只能够在定义函数时使用splat,并且还能够在调用函数时使用splat。
# 调用函数时使用splat func3 = (a,b,c) -> len = arguments.length console.log len if len == 1 a else if len == 2 a + ',' + b else if len == 3 a + ',' + b + ',' + c else 'null' arr = [1,2,3] console.log func3 arr... ### 输出: 3 1,2,3 ### # 被调用函数内部使用splat func4 = (a,b...) -> console.log Math.min b... func4 1, 2, 3, 4 # 输出2
调用splat总结: