다른 언어의 3항 연산자와 같이 동작 합니다. friday값이 참일때는 sue 아닐때는 jill입니다.
example-05-03.coffee
1
date = iffridaythensueelsejill
Splats…
…(점3개) Array를 잘라 낼수 있습니다. awardMedals contenders…contenders를 분할하여 awardMedals에 정의 됩니다. 여러개의 값이 있고 3개의 변수가 정의 되어 있을때 분할된 첫번째와 두번째는 개별 인자로 정의 되고 나머지 변수에 남은 Array값이 저장 됩니다.
`num`값이`0`될때까지횟수만큼문자열을반환합니다.# Nursey Rhymenum = 6lyrics = whilenum-=1"#{num} little monkeys, jumping on the bed. One fell out and bumped his head."
coffeescript do의 대한 설명
When using a JavaScript loop to generate functions,
it's common to insert a closure wrapper
in order to ensure that loop variables are closed over,
and all the generated functions don't just share the final values.
CoffeeScript provides the `do` keyword,
which immediately invokes a passed function, forwarding any arguments.
try계산 하고 error가 catch되면 try된 값이 아닌 catch값이 리턴 됩니다.
example-17.coffee
123456
alert(trynonexistent/underfinedcatcherror"And the error is … ${error}")
Operators and Aliases
CoffeeScript | JavaScript
is | ===
isnt | !==
not | !
and | &&
or | ||
true, yes, on | true
false, no, off | false
@, this | this
of | in
in | no JS equivalent
yeti가 undefiend나 null이 아닐 경우 yeti값을 반환하고 아니라면 bear 를 반환 합니다.
3항 연산자와 같은 형식 입니다.
example-19-01.coffee
1
footprints = yeti?"bear"
coffeescript의 ?.과 .
The accessor variant of the existential operator ?.
can be used to soak up null references in a chain of properties.
Use it instead of the dot accessor . in cases where the base value may be null or undefined.
If all of the properties exist then you'll get the expected result,
if the chain is broken, undefined is returned instead of the TypeError that would be raised otherwise.
example-20.coffee
1
zip = lottery.drawWinner?().address?.zipcode
Classes, Inheritance, and Super
class를 선언하고 불러 올수 있습니다.
new [class name]을 선언해서 class를 사용 합니다.
사용된 Snake와 Horse에서 extends Animal을 사용 하였으므로
class Animal을 사용 합니다.
example-21.coffee
123456789101112131415161718192021
classAnimalconstructor: (@name) ->move: (meters)=>alert@name+" moved #{meters}m."classSnakeextendsAnimalmove: ->alert"Slithering…"super5classHorseextendsAnimalmove: ->alert"Galloping…"super45sam = newSnake"Sammy the Python"tam = newHorse"Tommy the Palomino"sam.move()tom.move()
prototype ‘::’
::은 .prototype.의 줄임 표현 입니다.
String::dasherize는 String.prototype.dasherize로 변환 됩니다.
String Interpolation, Block Strings, and Block Comments
#{}은 문자열 보간입니다. #{ number / number}사용시 문자로 취급합니다.
example-31.coffee
1234
quthor = "Wittgenstein"quote = "A picture is a fact. -- #{author}"sentence = "#{22/7} is a decent approximation of ㅠ"
coffeescript 는 여러 문장의 개행을 인식 하지 않습니다.
example.32
123456
mobyDick = "Call me Ishmael. Some years ago -- never mind how long precisely -- having little or no money in my purse, and nothing particular to interset me on shore, I thought I would sail about a little and see the watery part of the world…"
Javascript 변환시 개행문자를 추가 하려면 """사용해 주어야 합니다.
example-33.coffee
12345
html = """ <strong> cup of coffeescript </strong> """
coffeescript의 여러줄 주석 입니다.
example-34.coffee
1234
###CoffeeScript Compiler v1.4.0Released under the MIT License###