machine =
running: no
turnOn: -> this.running = yes
turnOff: -> this.running = no
###
if not === unless
###
if not machine.running
machine.turnOn()
machine.turnOne() if not machine.running
console.log machine.running
unless machine.running
machine.turnOn()
else
machine.turnOff()
// Generated by CoffeeScript 1.4.0
(function() {
var machine;
machine = {
running: false,
turnOn: function() {
return this.running = true;
},
turnOff: function() {
return this.running = false;
}
};
/*
if not === unless
*/
if (!machine.running) {
machine.turnOn();
}
if (!machine.running) {
machine.turnOne();
}
console.log(machine.running);
if (!machine.running) {
machine.turnOn();
} else {
machine.turnOff();
}
}).call(this);
// 결과 :9번째 줄과 12번째 줄은 같은 구문이다
D:\Development\coffeescript>coffee if2
true
- switch ~ then 구문 사용하기 : switch.coffee
person =
name: "dowon"
job: "programmer"
giveWork = (person) ->
switch person.job
when "programmer"
console.log "1> Here's your code work, #{person.name}"
when "designer"
console.log "1> Here's your design work, #{person.name}"
else
console.log "1> Um, do you work here?"
giveWork person
# or
person.job = "designer"
giveWork2 = (person) ->
switch person.job
when "programmer" then console.log "2> Here's your code work, #{person.name}"
when "designer" then console.log "2> Here's your design work, #{person.name}"
else console.log "2> Um, do you work here?"
giveWork2 person
// Generated by CoffeeScript 1.4.0
var giveWork, giveWork2, person;
person = {
name: "dowon",
job: "programmer"
};
giveWork = function(person) {
switch (person.job) {
case "programmer":
return console.log("1> Here's your code work, " + person.name);
case "designer":
return console.log("1> Here's your design work, " + person.name);
default:
return console.log("1> Um, do you work here?");
}
};
giveWork(person);
person.job = "designer";
giveWork2 = function(person) {
switch (person.job) {
case "programmer":
return console.log("2> Here's your code work, " + person.name);
case "designer":
return console.log("2> Here's your design work, " + person.name);
default:
return console.log("2> Um, do you work here?");
}
};
giveWork2(person);
###
Math.random()
###
rand = (max = 10, min = 0) -> Math.floor(Math.random() * (max - min + 1)) + min
console.log rand()
console.log rand 100
console.log rand 60, 50
// 결과 : 수행때 마다 틀린 random값이 지정 범위내에서 출력됨
D:\Development\coffeescript>coffee random
5
100
53
D:\Development\coffeescript>coffee random
1
32
53
3) 비교연산
- ===, !== 사용하기
- if 문 사용하기
- ===, !== 를 사용하는 대신 is, isnt 문장으로 사용하기 : operator.coffee
###
is ===
isnt !==
###
name = "dowon"
if name is "dowon"
console.log "great"
if name isnt "Dowon"
console.log "poor"
### if( !false ) ###
if not false
console.log "cool"
###
var name = "dowon",
job = "programmer";
if(name === "dowon" || job === "programmer") {
console.log("right")
}
###
name = "dowon"
job = "programmer"
if name is "dowon" and job is "programmer"
console.log "right"
// 결과
D:\Development\coffeescript>coffee operator
great
poor
cool
right
- ? 연산자를 통하여 null 체크 하기 : operator2.coffee
name = "dowon"
if name?
console.log "hi"
person =
name: "dowon"
job: "programmer"
console.log person?.name
name = name || "youngsik"
name ||= "youngsik"
name ?= "youngsik"
console.log name
// Generated by CoffeeScript 1.4.0
(function() {
var name, person;
name = "dowon";
if (name != null) {
console.log("hi");
}
person = {
name: "dowon",
job: "programmer"
};
console.log(person != null ? person.name : void 0);
name = name || "youngsik";
name || (name = "youngsik");
if (name == null) {
name = "youngsik";
}
console.log(name);
}).call(this);
// 결과
D:\Development\coffeescript>coffee operator2
hi
dowon
dowon
- if 문을 재구성한다 : if.coffee
# x = 4
# if( x >= 0 && x <= 10) {}
x = 4
if 0 <= x <= 10
console.log "true"
// Generated by CoffeeScript 1.4.0
(function() {
var x;
x = 4;
if ((0 <= x && x <= 10)) {
console.log("true");
}
}).call(this);
C:\Documents and Settings\UserXP\Application Data\npm\cake-> C:\Documents and Settings\UserXP\Application Data\npm\node_modules\coffee-script\bin\cake
C:\Documents and Settings\UserXP\Application Data\npm\coffee -> C:\Documents and Settings\UserXP\Application Data\npm\node_modules\coffee-script\bin\coffee
coffee-script@1.4.0 C:\Documents and Settings\UserXP\Application Data\npm\node_m
odules\coffee-script
D:\Development\javascript>coffee -v
CoffeeScript version 1.4.0
2. Coffee 사용하기
- 확장자 .coffee 파일을 만든다
- coffee 명령으로 수행할 수도 있고, js 파일로 컴파일 할 수도 있다
- 일반적인 js 코딩 : arr.js
function makeArray(dimension) {
var arr = [], i = 0, j = 0;
for(; i < dimension; i++) {
arr[i] = [];
for( j=0; j < dimension; j++) {
arr[i][j] = '1111';
}
}
return arr;
}
var myArr = makeArray(4);
console.log(myArr);
// 결과
D:\coffeescript>node arr
[ [ '1111', '1111', '1111', '1111' ],
[ '1111', '1111', '1111', '1111' ],
[ '1111', '1111', '1111', '1111' ],
[ '1111', '1111', '1111', '1111' ] ]
- CoffeeScript 코딩 방식 : arr.coffee (coffee 확장자)
makeArray = (dimension) ->
arr = []
for i in [0...dimension]
arr[i] = []
arr[i][j] = '1111' for j in [0...dimension]
arr
myArr = makeArray 4
console.log myArr
console.log 'dowon hi'