CoffeeScript函數(shù)

2018-08-23 15:20 更新
在 CoffeeScript 中函數(shù)通過“->”符號來定義。以下是一個簡單的函數(shù):
hi = -> “Hello ,World!”
如果要定義一個帶參數(shù)的函數(shù),則可以這樣定義:
greeting = (name) -> “Hello, #{name}!”
可見只要在’->’ 之前添加類是函數(shù)定義時的(arguments)就可以為函數(shù)提供參數(shù),并且通過在字符串中使用 #{} 引用參數(shù)變量就可以了。
如果可能有大量參數(shù)需要傳遞,也可以使用 arguments 對象:
greeting = –> “Hello, #{arguments[0]}!”
Javascript 中的標(biāo)準(zhǔn)函數(shù)在 CoffeeScript 也同樣是兼容的,比如 Math
cube = (num) –> Math.pow num,3
注意到函數(shù)運行時并不要求使用 () 將參數(shù)包圍。
如果傳遞的參數(shù)并不符合逾期可以拋出異常以獲取問題出現(xiàn)的原因。
odd = (num) ->
if typeof num is ‘number’
if num is Math.round num
if num > 0
num % 2 is 1
else
throw “#{num} is not positive”
else
throw “#{num} is not an integer”
else
throw “#{num} is not a number”
要獲取到程序拋出的錯誤信息,直接運行函數(shù)是不行的,可以使用 try..catch 語句獲?。?br />try
odd 5.1
catch e
console.log e
當(dāng)然也可以通過使用 unless 重寫 odd 函數(shù):
odd = (num) ->
unless typeof num is ‘number’
throw “#{num} is not positive”
unless num is Math.round num
throw “#{num} is not an integer”
unless num > 0
throw “#{num} is not a number”
num % 2 is 1
如果覺得這樣的格式還太長了,也可以使用類似 ruby 的語法,使用 throw A unless B 的格式編寫這個函數(shù)。
函數(shù)中還可以修改變量、運行其他函數(shù),javascript 中的函數(shù)能做到的 CoffeeScript 中的函數(shù)一樣能夠做到。
count = 0
anyfunc = -> count++
anyfunc()

console.log count  #now count is 1



函數(shù)聲明
CoffeeScript的函數(shù)聲明很有意思,函數(shù)通過一組可選的圓括號包裹的參數(shù), 一個箭頭, 一個函數(shù)體來定義的。就像下面這樣:


#編譯前
square = (x) ->
  x * x


//編譯后
var square;


square = function(x) {
  return x * x;
};
函數(shù)體另起一行來寫,千萬別忘了縮進(jìn)代碼,因為CoffeeScript是用縮進(jìn)來區(qū)分代碼塊的。如果你不縮進(jìn)的話,就是這個樣子的:


#編譯前
square = (x) ->
x * x


//編譯后
var square;    
square = function(x) {};    
x * x;
如果函數(shù)沒有參數(shù)的話,如下:


#編譯前
square = ->
  x * x


#包括參數(shù)的括號可要可不要
square =() ->
  x * x


//編譯后
var square;    
square = function() {
  return x * x;
}; 
多個參數(shù)用逗號隔開:


#編譯前
square = (x, y) ->
  x * y
//編譯后
var square;


square = function(x, y) {
  return x * y;
};
一個立即執(zhí)行的匿名函數(shù)可以這樣寫:


#編譯前
(->)()


//編譯后
(function() {})();
CoffeeScript在編譯后的函數(shù)體體會給你最后的代碼加上一個return,如果你不想要這個return值得話,可以顯式的return一個值:


#編譯前
square = (x, y) ->
  x * y
  return 0


//編譯后
var square;


square = function(x, y) {
  x * y;
  return 0;
};
函數(shù)調(diào)用
函數(shù)的調(diào)用和JavaScript的調(diào)用方式一樣:


#編譯前
str = ->
  return 'xxx'


str()


//編譯后
var str;


str = function() {
  return 'xxx';
};


str();
如果函數(shù)有參數(shù)的話,可以省掉括號不寫。沒有參數(shù)的話,括號必不可少?。?br />

#編譯前
square = (x, y) ->
  x * y


square 2,4


//編譯后
var square;


square = function(x, y) {
  return x * y;
};


square(2, 4);
函數(shù)要先聲明,后調(diào)用!


默認(rèn)參數(shù)
一些函數(shù)函數(shù)參數(shù)會有默認(rèn)值, 當(dāng)傳入的參數(shù)的不存在時會被使用。


#編譯前
square = (x, y = 2) ->
  x * y


square 3


//編譯后
var square;


square = function(x, y) {
  if (y == null) {
    y = 2;
  }
  return x * y;
};


square(3);
如果有多個參數(shù)的話,必填參數(shù)在前,默認(rèn)參數(shù)在后!大家想想為啥?


變參
JavaScript函數(shù)里可以使用arguments類數(shù)組對象獲取不定參數(shù)。CoffeeScript在函數(shù)定義和調(diào)用里提供了變參的語法, 讓不定個數(shù)的參數(shù)使用起來更愉悅一些。廢話不多說,看例子:


#編譯前
square = (x, y, z...) ->
  #todo


square 1,2,3,4,5


//編譯后
var square,
  __slice = [].slice;


square = function() {
  var x, y, z;
  x = arguments[0], y = arguments[1], z = 3 <= arguments.length ? __slice.call(arguments, 2) : [];
};


square(1, 2, 3, 4, 5);
變參一定要放到最后,看編譯后的代碼,第一個參數(shù)賦值給了x,第二個參數(shù)賦值給了y,剩余的參數(shù)被封裝成一個數(shù)組給了z。實際上變參接受的是多余參數(shù)的數(shù)組集合。


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號