다른 언어의 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###
#head_checkbox는 최상의 checkbox 컨트롤러 이며 하위 checkbox 들을 조정 합니다.
체크시 하위 모든 checkbox가 모두 클릭 되며 미클릭시 하위 checkbox 또한 미클릭으로 전환 합니다.
12345678910
$('#head_checkbox').click(function(){//find all checkboxesvarcbs=$("input:checkbox");//화면상의 모든 Checkbox 를 찾아서 Head Checkbox 상태와 동기화 합니다. (대장이 체크면 쫄도 모두다 채크)for(vari=0;i<cbs.length;i++){if(cbs[i].type=="checkbox"){cbs[i].checked=$(this).is(':checked');}}});
$content값이 참인지 분별하여 거짓이면 Second content?값을 부여 합니다.
참이므로 $content는 First..가 됩니다.
$new_content는 거짓 이므로 First time…을 출력 합니다.
example-26.scss
12345678
$content:"First content";$content:"Second content?"!default;$new_content:"First time reference"!default;#main{ccontent:$content;new-content:$new_content;}
example-26.css
1234
#main{content:"First content";new-content:"First time reference";}
Class selectors aren’t the only things that can be extended. It’s possible to extend any selector involving only a single element, such as .special.cool, a:hover, or a.user[href=”http://”]
Normally when you extend a selector, it’s an error if that @extend doesn’t work. For example, if you write a.important {@extend .notice}, it’s an error if there are no selectors that contain .notice. It’s also an error if the only selector containing .notice is h1.notice, since h1 conflicts with a and so no new selector would be generated.
Sometimes, though, you want to allow an @extend not to produce any new selectors. To do so, just add the !optional flag after the selector.
example.scss
123
a.important{@extend.notice!optional;}
@extend in Directives
There are some restrictions on the use of @extend within directives such as @media. Sass is unable to make CSS rules outside of the @media block apply to selectors inside it without creating a huge amount of stylesheet bloat by copying styles all over the place. This means that if you use @extend within @media (or other CSS directives), you may only extend selectors that appear within the same directive block
@mixin adjust-location($x,$y){@ifunitless($x){@warn"Assuming #{$x} to be in pixels";$x:1px*$x;}@ifunitless($y){@warn"Assuming #{$y} to be in pixels";$y:1px*$y;}position:relative;left:$x;top:$y;}
$ ./create <project_folder_path> pheonegap.start start
$ cd ~/project_folder_path
$ ls
AndroidManifest.xml cordova project.properties
ant.properties gen res
assets libs src
bin local.properties
build.xml proguard-project.txt
project_name.xcodeproj 파일을 클릭 하면 Xcode가 실행 됩니다.
실행후 컴파일 시간을 거친후 아래와 같이 Run 우측에
project_name > iPhone v.xx Simulator 선택 한후 Run(단축키 Command + R)을 실행 시켜 줍니다.
<html><head><script src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js"type="text/javascript"charset="utf-8"></script><script type="text/coffeescript">fill=(container,liquid="coffee")->"Filling the #{container} with #{liquid}..."alertfill("cup")</script></head><body></body></html>
coffeescript - .js 확인하기
.html 파일 내에서 변환된 .js 내용을 검사 할수있습니다.
coffee.html
123456789101112131415161718192021
<html><head><script src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js"type="text/javascript"charset="utf-8"></script><script>varfill;fill=function(container,liquid){if(liquid==null){liquid="coffee";}return"Filling the "+container+" with "+liquid+"...";};alert(fill("cup"));</script></head><body></body></html>
ruby를 설치 하기전 ruby를 관리해주는 RVM 을 설치 합니다.
처음 실행 시키면 q를 누르라고 나옵니다.
q 입력후 설치 시간이 좀 걸리게 되며 아래와 같은 내용이
나올때까지 기다려야 합니다.
$ curl -L https://get.rvm.io | bash -s stable --ruby
ruby-1.9.3-p327 - #extracting ruby-1.9.3-p327 to /Users/hancho/.rvm/src/ruby-1.9.3-p327
ruby-1.9.3-p327 - #extracted to /Users/hancho/.rvm/src/ruby-1.9.3-p327
ruby-1.9.3-p327 - #configuring
ruby-1.9.3-p327 - #compiling
ruby-1.9.3-p327 - #installing
Removing old Rubygems files...
Installing rubygems-1.8.24 for ruby-1.9.3-p327 ...
Installation of rubygems completed successfully.
Saving wrappers to '/Users/hancho/.rvm/bin'.
ruby-1.9.3-p327 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
ruby-1.9.3-p327 - #importing default gemsets (/Users/hancho/.rvm/gemsets/)
Install of ruby-1.9.3-p327 - #complete
Creating alias default for ruby-1.9.3-p327.
Recording alias default for ruby-1.9.3-p327.
Creating default links/files
Saving wrappers to '/Users/hancho/.rvm/bin'.
* To start using RVM you need to run `source /Users/hancho/.rvm/scripts/rvm`
in all your open shell windows, in rare cases you need to reopen all shell windows.
Ruby 설치
RVM이 설치가 정상적으로 완료되면 rvm 을 사용해 루비를 설치해 줍니다.
$ rvm install 1.9.3
A RVM version 1.16.20 (stable) is installed yet 1.14.5 (stable) is loaded.
Please do one of the following:
* 'rvm reload'
* open a new shell
* 'echo rvm_auto_reload_flag=1 >> ~/.rvmrc' # for auto reload with msg.
* 'echo rvm_auto_reload_flag=2 >> ~/.rvmrc' # for silent auto reload.
ruby 버전별 사용
ruby 버전을 최신으로 설정해 줍니다.
$ rvm use 1.9.3
Using /Users/hancho/.rvm/gems/ruby-1.9.3-p327
ruby 최신 버전 적용
현재 보다 최신 버전이 있으면 최신 버전으로 적용 시켜 주게 됩니다.
현재 사용하고 있는 버전이 최신이 아니었다면 위의 $ rvm use 1.x.x 사용
하는게 좋습니다.
$ rvm rubygems latest
Removing old Rubygems files...
Installing rubygems-1.8.24 for ruby-1.9.3-p327 ...
Installation of rubygems completed successfully.
Sass 설치
RVM과 Ruby가 정상적으로 설치되면 sass를 설치 할수 있습니다.
$ gem install sass
Sass 사용방법
Sass 따라 해보기
테스트 폴더를 생성후 `style.scss` 파일을 만듭니다.
$ mkdir Sass-Start
$ cd Sass-Start
$ touch style.scss