CoffeeScript:10 個提高生產力的技巧與實例
有些人可能不知道 CoffeeScript,它最終將編譯成 JavaScript,今天我們要給出 10 個提高 CoffeeScript 生產力的技巧和實例。
Tip 1: 可變參數個數
CoffeeScript 允許使用可變參數個數,下面是一個簡單例子:
mySplat = (args...) -> console.log "#{args.join()}" mySplat(1,2,3,4,5)
輸出結果:
1,2,3,4,5
Tip 2: 使用布爾型變量
你可以使用 on 和 yes 相當于 true, 使用 off 和 no 相當于 false
lightSwitch = false if lightSwitch is off console.log 'Turn on the lights!' else console.log 'Turn off the lights!'
輸出結果:
Turn on the lights!
Tip 3: 存在操作符
這是用來檢測一個變量是否存在的方法:
myValue1 = 'Hello World' if myValue1? then console.log myValue1 # This next variable doesn't exist if myValue2? then console.log myValue2
輸出結果:
Hello World
你也可以這樣做:
class MyClass myFunction1: (val) -> console.log "You passed #{val} to myFunction1" myClass = new MyClass() myClass?.myFunction1(123) # This next function doesn't exist myClass.myFunction2?(123)
一旦 myFunction2 不存在于 MyClass 類中,你就可以看到如下輸出:
You passed 123 to myFunction1
Tip 4: 使用 ‘in’ 來測試數組中是否包含某個值
通過關鍵字 in 你可以快速的檢查數組中是否包含某個值
foods = ['apple', 'orange', 'potatoe', 'strawberries'] if 'potatoe' in foods then console.log 'Found potatoe' if 'carrot' in foods then console.log 'Found carrot'
輸出結果:
Found potatoe
Tip 5: 對象迭代
我們創建一個簡單的對象,并展示迭代是多么的簡單:
dragon = level: 1 alignment: 'neutral' age: 'Youngling' attack: 'Fire' damage: '1d4' for key, val of dragon console.log "#{key}: #{val}"
輸出結果:
level: 1 alignment: neutral age: Youngling attack: Fire damage: 1d4
Tip 6: 類
類是非常簡單的,下面創建一個簡單的類并演示使用構造器和繼承:
class Spaceship constructor: (@speed = 1, @spaceShipType = 'spaceship') -> console.log "New #{@spaceShipType} created with a speed of #{@speed}" move: () -> console.log "The #{@spaceShipType} is moving now at a speed of #{@speed}" class FlyingSaucer extends Spaceship constructor: -> super 5, 'Flying Saucer' useDeathRay: -> console.log "The flying saucer is using a death ray!" spcshp = new Spaceship() spcshp.move() flySaucr = new FlyingSaucer() flySaucr.move() flySaucr.useDeathRay()
Tip 7: ‘this’ 的快捷用法
你可以使用 @ 符號來代替 this
class MyClass constructor: (@greeting) -> console.log "You set the greeting as: #{@greeting}" greet: -> console.log "You said: #{@greeting}" myClass = new MyClass('Bonjour') myClass.greet()
你可看到 @greeting 是一個有效的類函數。
輸出結果:
You set the greeting as: Bonjour You said: Bonjour
Tip 8: 默認值
實際上,上面的代碼中我在構造器中設置了一個默認的 greeting 值,這里做個小改動:
class MyClass constructor: (@greeting = 'Hola') -> console.log "You set the greeting as: #{@greeting}" greet: -> console.log "You said: #{@greeting}" myClass = new MyClass() myClass.greet()
運行結果:
You set the greeting as: Hola You said: Hola
Tip 9: 字符串塊
如果你想對字符串進行引號和撇的快速轉義,可使用如下方法:
markup = """ <form action="/" method="post"> <input type="submit" /> </form> """ console.log markup
輸出:
<form action="/" method="post"> <input type="submit" /> </form>
Tip 10: 字符串插值
我在我的很多例子都用到了這個方法,在雙引號中的所有內容都將被解析,你可以在其中嵌入某個變量:
myValue = 'Hello World' console.log "You said: #{myValue}"
或者是:
val1 = 5; val2 = 7; console.log "Your answer for 5 * 7 is: #{val1 * val2}"
輸出結果:
Your answer for 5 * 7 is: 35
你也可以這樣:
console.log "Your answer for 5 * 7 is: #{5 * 7}"
英文原文,OSCHINA原創翻譯
本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!