블로그 이미지
윤영식
Full Stacker, Application Architecter, KnowHow Dispenser and Bike Rider

Publication

Category

Recent Post

2013. 3. 30. 12:33 Languages/CoffeeScript

CoffeeScript의 컴파일과 Minify, 하나의 파일로 만들기 그리고 자동 변경체크하여 컴파일 하기를 좀 더 쉽게 하는 Coffeebar를 알아보자 



1) Coffeebar 기능과 설치

  - 기능

Supports Literate CoffeeScript

Robust file watching

Cross-platform

Minification

Concatenation of multiple source files

Shows the actual source line when concatenated files have a compile error


  - 설치

$ sudo npm install -g coffeebar

/usr/local/bin/coffeebar -> /usr/local/lib/node_modules/coffeebar/bin/coffeebar

coffeebar@0.2.0 /usr/local/lib/node_modules/coffeebar

├── mkdirp@0.3.5

├── xcolor@0.1.0

├── coffee-script@1.6.2

├── commander@1.1.1 (keypress@0.1.0)

├── glob@3.1.21 (inherits@1.0.0, graceful-fs@1.2.0, minimatch@0.2.11)

├── beholder@0.1.2 (async@0.2.6, minimatch@0.2.11)

└── uglify-js@2.2.5 (optimist@0.3.5, source-map@0.1.19)



2) CLI 사용법

  - 옵션들

Usage: coffeebar [options] [path ...]


Options:


  -h, --help           output usage information

  -V, --version        output the version number

  -b, --bare           compile without a top-level function wrapper

  -m, --minify         minify output files

  -o, --output <path>  output path

  -s, --silent         suppress console output

  -w, --watch          watch files for changes


  - 컴파일 하기

$ coffeebar test.coffee


  - 소스 디렉토리(src)를 지정하여 컴파일 디렉토리(lib) 지정하기 

$ coffeebar src -o lib


  - 소스 디렉토리(src)를 지정하여 하나의 파일로 합치기

$ coffeebar src -o joined.js


  - 소스 디렉토리(src)를 지정하여 컴파일 디렉토리(lib) 지정하고 .coffee 소스 파일 변경되면 자동 컴파일하기 

$ coffeebar src -o lib -w



3) API 사용법

  - coffeebar(inputPaths, [options]) : inputPaths에 소스 디렉토리 지정하면 options에 따라 수행을 결정한다 

  - 옵션들

bare - (boolean) CoffeeScript compiler option which omits the top-level function wrapper if set to true.

minify - (boolean) Minify output files. Defaults to false.

output - (string) The path to the output file. If the path has a file extension, all files will be joined at that location. Otherwise, the path is assumed to be a directory.

silent - (boolean) Suppress all console output. Defaults to true.

watch - (boolean) Watch all files and directories for changes and recompile automatically. Defaults to false.


  - 예 : output 위치와 합칠 파일을 지정

// use the api in source

var coffeebar = require('coffeebar');

coffeebar('src', {output: 'lib/app.js'});


$ npm install

$ npm test



<참조>

  - 원문 : nmpjs.org coffeebar

posted by 윤영식
2013. 1. 31. 23:06 Languages/CoffeeScript

CoffeeScript 문법중에서 꼭 알아야 할 것들에서 정리한다. 


1) Variables

  - 변수 선언을 하지 않는다 : 자동으로 var 붙음 

  - 세미콜론 ; 을 쓰지 않는다 

  - () 넣는 것은 옵션이다

 

2) Function

  - Named Function 과 Function Expression이나 둘다 coffee(); 를 호출한다 

  - Named Function을 Function Expression으로 변환하여 표현한다 (참조)

/////////////////////////

// Named Function

function coffee() {

  return confirm('dowon');

}


/////////////////////////

// Function Expression

var coffee = function() {
  return confirm('dowon');
}

/////////////////////////

// CoffeeScript 변환 

1) var 제거 

2) function() 을 -> 변환

3) {..} 컬리브레이스 와 ; 세미콜론 제거 


coffee = -> 

  confirm 'dowon'


4) confirm 은 1 tab 또는 2 space 로 들여쓰기를 한다 

5) CoffeeScript는 항상 return value를 갖는다 

6) 컴파일 되면 Function Expression과 동일하다 


3) String 변환

  - #{...} 사용한다 

/////////////////////////

// CoffeeScript

coffee = ->

   answer = confirm 'dowon'

   "hi #{answer}"


/////////////////////////

// Function Expression

var coffee;

coffee = function() {

  var answer;

  answer = confirm('dowon');

  return "hi " + answer;

}


4) Function Parameters

 ////////////////////////

coffee = ->    호출 : coffee()


////////////////////////

// ( ) 넣는 것은 옵션

coffee = (message) ->     

   호출 : coffee("dowon") 또는 coffee "dowon"  


coffee = (message, other) ->   

   호출 : coffee("hi", 2)  또는 coffee "hi", 2 


///////////////////////

// 파라미터 기본 할당

coffee = (message = "dowon") ->

   answer = confirm message

   "hi #{answer}"


호출 : alert coffee()  또는 alert coffee("youngsik")


5) CoffeeScript CLI (Command Line)

  - coffee -c test.coffee : test.js 컴파일 

  - coffee -cw test.coffee : 내역이 업데이트 될 때마다 재컴파일 

  - coffee -c src -o js : src 디렉토리에 있는 .coffee 파일을 컴파일해서 js 디렉토리로 옮긴다 

  - coffee -wc src -o js : 파일이 업데이트 되면 재컴파일 한다 


6) jQuery 사용하기 

////////////////////////////////

// jQuery 원본 

jQuery(function($) {

   function changeTab(e) {

     e.preventDefault();

     $(#tabs li a.active").removeClass("active");

     $(this).addClass("active")

  }


  $("#tabs ul li a").click(changeTab);

});


////////////////////////////////

// CoffeeScript 변환   

$ ->

  changeTab = (e) ->

     e.preventDefault()

     $(#tabs li a.active").removeClass "active"

     $(@).addClass "active"


   $("#tabs ul li a").click changeTab

  


  - jQuery(function($) {   ==>   jQuery ($) -> 또는 $ -> 

  - function changeTab(e)  ==>  changeTab = (e) -> 

  - ( ) 와 ; 생략 

  - this ==> @ 변환 



7) if 문

  - ( ) 과 { } 를 제거 한다

  - 한줄 표현시 수행 문구를 앞으로 놓을 수 있다

  - 한줄 표현시 수행 문구가 뒤로 가면 then을 사용한다 

//////////////////

// JavaScript 

if (age < 18) {

  alert('dowon');

}


// 1차 변환 

if age < 18 

  alert 'dowon'


// 2차 변환 

alert 'dowon' if age < 18


// 3차 변환

if age < 18 then alert 'dowon'


/////////////////

// if else 구문 

if (age < 18) {

  alert('yun');

} else {

  alert('dowon');

}


// 1차 변환

if age < 18

  alert 'yun'

else

  alert 'dowon'


// 2차 변환 

if age < 18 then alert 'yun' else alert 'dowon'


8) Operator 

  - 주로 if문에 많이 사용

  

/////////////////////////

// CoffeeScript 

if paid() and coffee() is on then pour()


// JavaScript 변환 

if(paid() && coffee() === true) {

  pour();

}


/////////////////////////

addCaffeine() if not Decaf()   ===  addCaffeine() unless Decaf()


/////////////////////////

// JavaScript

if(2 < dodo && dodo < 5) {

  alert('youngsik');

}


// CoffeeScript 1차 

if 2 < dodo < 5

  alert 'youngsik'


// 2차 

if 2 < dodo < 5 then alert 'youngsik'


9) Switch 문 변환 

  - case <조건>: 을 when <조건> then 으로 변환 

////////////////////////

// JavaScript 

var message = (function() {

  switch(coffee) {

    case 0:

       return 'hi';

    case 1:

       return 'haha';

    default:

       return 'dowon';

  }

})();


//////////////////////

// CoffeeScript 변환

message = switch coffee

   when 0 then 'hi'

   when 1 then 'haha'

   else 'dowon'


10) undefined 와 null 체크 하기 변환 

  - ? 로 변환 

////////////////////////

// JavaScript

if (typeof coffee !== "undefined" && coffee !== null) {

  alert("dowon");

}


// CoffeeScript 1차 변환 

if coffee?

  alert "dowon"


// CoffeeScript 2차 변환

alert "dowon" if coffee?


11) Existential 연산 변환

  - if not 을 unless 변환

//////////////////////////////////////////

// null 또는 undefined 일 때 0 으로 설정

// 1차 

if not coffee?

  coffee = 0 


// 2차

coffee = 0 unless coffee?


// 3차 

coffee ?= 0 


////////////////////////////////////////

// null 또는 undefined 아니면 호출하기 

// 즉, 그것이 존재하면 function을 호출

// 1차

if coffee?

   coffee.brew()


// 2차

coffee?.brew()


12) 배열 변환 

////////////////////////////

// 변수 할당

range = [1..4]  ==>  var range = [1, 2, 3, 4];


////////////////////////////

// 변수 할당 

range = [1...4]  ==> var range = [1, 2, 3];


////////////////////////////

// 변수 사용

start = 5

end = 10

range = [start..end]

          값 [5, 6, 7, 8, 9, 10]


range[1..4] 

          값 [6, 7, 8, 9]


range[1...range.length]  이것은 요것과 동일 range[1..-1]

          값 [6, 7, 8, 9, 10] 


////////////////////////////

// 배열 콤마 제거 

locations = ['seoul', 'kyunggi', 'jeju']

또는

locations = [

  'seoul'

  'kyunggi'

  'jeju'

]


13) Loop 문 변환 

  - forEach 문 사용

  - for .. in 문 사용 

////////////////////////////////////

// CoffeeScript

// forEach 문 

locations = ['seoul', 'kyunggi', 'jeju']

locations.forEach(location, index) ->

  alert "location: #{location}"


// for .. in 문 1차 

for location in locations

  alert "location: #{location}"


// for .. in 문 2차 

alert "location: #{location}" for location in locations


////////////////////////////////////

// JavaScript

locations.forEach(function(location, index) {

  return alert("location: " + location);

});


to be continue...

posted by 윤영식
2013. 1. 29. 21:48 Languages/CoffeeScript

정규표현식과 html안에 .coffee 확장자로 작성한 스크립트를 포함시키는 방법에 대하여 알아보자 


1) 정규 표현식 

  - /// .....  ///   슬래쉬3개 앞뒤로 하여 그사이에 표현식을 넣는다 

//////////////////////////////////////////
// regex.coffee
emails = ['ysyun@yuwin.co.kr', 'nulpulum@gamil.com', 'joe.567@gmail.com', 'andrew']

emailRegex = ///
	([\w\.+-]+) 	# unique name
	@				# at-sign
	(\w+)			# domain name
	(\.\w+[\w\.]*)  # tld
///

for email in emails
	match = email.match emailRegex
	if match?
		console.log "#{email} matched"
	else
		console.log "#{email} didn't matched"

//////////////////////////////////////////
// 컴파일 내역
// Generated by CoffeeScript 1.4.0
var email, emailRegex, emails, match, _i, _len;
emails = ['ysyun@yuwin.co.kr', 'nulpulum@gamil.com', 'joe.567@gmail.com', 'andrew'];
// 한줄로 표현된다 
emailRegex = /([\w\.+-]+)@(\w+)(\.\w+[\w\.]*)/;

for (_i = 0, _len = emails.length; _i < _len; _i++) {
  email = emails[_i];
  match = email.match(emailRegex);
  if (match != null) {
    console.log("" + email + " matched");
  } else {
    console.log("" + email + " didn't matched");
  }
}


2) Coffeescript를 컴파일한 Javascript 파일을 포함한 경우의 html

  - src/dom.coffee 파일 작성

  - js/ 디렉토리 밑으로 컴파일된 .js 위치 : coffee -c --watch -o js src/dom.coffee

    + --watch 옵션 : 변경내용을 감시하여 자동 재컴파일 수행 

  - 브라우저에서 웹서버 도움없이 바로 index.html 파일을 호출하면 된다. 

    + 단, jquery.js 파일은 동일 디렉토리에 위치 (첨부파일 참조)

//////////////////////////////////////////
// index.html 파일 
<!DOCTYPE html>
<html>
	<head>
		<title> CoffeeScript in the Browser </title>
	</head>
	<body>
		<div id="menu">
			<div>Tools</div>
			<div id="dropdown">
				<ul>
					<li>Tool 1</li>
					<li>Tool 2</li>
					<li>Tool 3</li>
				</ul>
			</div>
		</div>
		<script src="jquery.js"></script>
		<script src="js/dom.js"></script> <!-- 컴파일한 dom.js 파일을 포함시킨다 -->
	</body>
</html>  
// dom.coffee 파일로 jquery 기반 코딩
$ -> 
	menu = $ '#menu'
	dropdown = $ '#dropdown'
	dropdown.hide()

	menu.on 'mouseover', (e) -> dropdown.stop().show 200
	menu.on 'mouseout', (e) -> dropdown.stop().hide 200

//////////////////////////////////////////
// 컴파일 내역
$(function() {
  var dropdown, menu;
  menu = $('#menu');
  dropdown = $('#dropdown');
  dropdown.hide();
  menu.on('mouseover', function(e) {
    return dropdown.stop().show(200);
  });
  return menu.on('mouseout', function(e) {
    return dropdown.stop().hide(200);
  });
});


3) dom.coffee 파일을 직접 html안에 포함시키기 

  - html안에 coffee-script.js 파일을 포함시킨다 (첨부파일 참조)

    + 다운로드 한 파일의 extras 디렉토리 파일임

  - dom.coffee 파일을 포함시키고 type="text/coffeescript" 타입을 넣는다 

//////////////////////////////////////////
// index2.html 파일 
<!DOCTYPE html>
<html>
	<head>
		<title> CoffeeScript in the Browser </title>
	</head>
	<body>
		<div id="menu">
			<div>Tools</div>
			<div id="dropdown">
				<ul>
					<li>Tool 1</li>
					<li>Tool 2</li>
					<li>Tool 3</li>
				</ul>
			</div>
		</div>
		<script src="jquery.js"></script>
		<script src="coffee-script.js"></script>  <!-- 컴파일한 coffee-script.js 파일을 포함 -->
		<script src="src/dom.coffee" type="text/coffeescript"></script>  <!-- dom.coffee 포함 -->
	</body>
</html>  

  - 브라우져에서 로컬파일을 불러오게 되면 오류가 발생한다

  - 실행방법 : 웹서버 구동하여 브라우져에서 호출 

    + node-static 을 설치한다 (블로그 참조)

    + $ static : dom.coffee 파일이 있는 곳에서 수행 (8080 포트로 브라우저에서 호출함)

     


  - 최종 파일 구조     

    

 

* 파일 

coffeescript-5.zip



<참조>

  - 자바스크립트 정규표현식

posted by 윤영식
2013. 1. 27. 23:22 Languages/CoffeeScript

Java 처럼 Class를 쉽게 만들어 보자. 클래스끼리 상속하여 재정의(overriding)하는 부분도 알아보자 


1) Class 만들기 

  - class 로 표현한다

  - 컴파일 내역에 IIFE(즉시실행함수)안에 다시 Dog을 생성하여 호출함 

// clazz.coffee
class Dog

// 컴파일 내역 : clazz.js 
// Generated by CoffeeScript 1.4.0
(function() {
  var Dog;
  Dog = (function() {
    function Dog() {}
    return Dog;
  })();
}).call(this);


  - constructor 로 생성자를 정의한다

  - constructor를 사용하면 Dog 생성자가 생성되고 다른 프로퍼티는 prototype에 정의된다 

// clazz2.coffee
class Dog
	constructor: (@name) ->
	growl: -> console.log '*growl*'

dog = new Dog 'Dowon'
console.log dog.name
dog.growl()

// 결과
D:\Development\coffeescript\4>coffee clazz2
Dowon
*growl*

// 컴파일 내역 : clazz2.js
// Generated by CoffeeScript 1.4.0
var Dog, dog;
Dog = (function() {
  function Dog(name) {
    this.name = name;
  }
  Dog.prototype.growl = function() {
    return console.log('*growl*');
  };
  return Dog;
})();

dog = new Dog('Dowon');
console.log(dog.name);
dog.growl();


2) 상속 관계 만들기 

  - extends 사용하여 상속관계 정의 한다

  - super() 사용하여 동일 메소드에 대한 재정의(Overriding)을 한다 

// clazz3.coffee
class Dog
	constructor: (@name) ->
	growl: -> console.log '*growl*'

class BorderCollie extends Dog
	constructor: (name, @tricks = []) ->
		super name
	perform: (trick) -> console.log if trick in @tricks then "#{@name} is #{trick}" else '*whine*'	
	growl: (person) ->
		if person is @master
			console.log '*bark*'
		else
			super() # Dog.growl()
 
dog = new Dog 'Dowon'

console.log dog.name
dog.growl()

dog2 = new BorderCollie 'YoungSik', ['playing', 'catching', 'rolling']
dog2.master = "Dowon"

console.log dog2.name
dog2.perform 'rolling'
dog2.growl("Dowon")
dog2.growl("Yun")

// 결과 
D:\Development\coffeescript\4>coffee clazz3
Dowon
*growl*
YoungSik
YoungSik is rolling
*bark*
*growl*

  - BorderCollie extends Dog 으로 상속 만들어 줌 

  - BorderCollie의 constructor안에 "super name" 을 호출하여 super(name)을 넣어 줌

  - BorderCollie의 grow안에서 super() 를 호출하여 super.growl()을 넣어 줌 

// 컴파일 내역 : clazz3.js 
// Generated by CoffeeScript 1.4.0
(function() {
  var BorderCollie, Dog, dog, dog2,
    __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
    __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };

  Dog = (function() {
    function Dog(name) {
      this.name = name;
    }
    Dog.prototype.growl = function() {
      return console.log('*growl*');
    };
    return Dog;
  })();

  BorderCollie = (function(_super) {
    __extends(BorderCollie, _super);
    function BorderCollie(name, tricks) {
      this.tricks = tricks != null ? tricks : [];
      BorderCollie.__super__.constructor.call(this, name);
    }
    BorderCollie.prototype.perform = function(trick) {
      return console.log(__indexOf.call(this.tricks, trick) >= 0 ? "" + this.name + " is " + trick : '*whine*');
    };

    BorderCollie.prototype.growl = function(person) {
      if (person === this.master) {
        return console.log('*bark*');
      } else {
        return BorderCollie.__super__.growl.call(this);
      }
    };
    return BorderCollie;
  })(Dog);

  dog = new Dog('Dowon');
  console.log(dog.name);
  dog.growl();

  dog2 = new BorderCollie('YoungSik', ['playing', 'catching', 'rolling']);
  dog2.master = "Dowon";
  console.log(dog2.name);
  dog2.perform('rolling');
  dog2.growl("Dowon");
  dog2.growl("Yun");

}).call(this);

* 파일 

coffeescript-4.zip


posted by 윤영식
2013. 1. 27. 21:14 Languages/CoffeeScript

객체의 function으로 호출될 때 this. 값을 어떻게 줄여서 표현 하는지와 prototype 도 알아보자. 또한 extends를 어떻게 사용하는지도 알아본다


1) this.name = name 표현하기 

  - this. 을 @ 로 표현한다 

// prototype.coffee
###
function Dog (name) {
	this.name = name;
}
Dog.prototype.growl = function() {
	console.log("*growl*");
}
r = new Dog("Dowon");
r.growl();
###

Dog = (name) -> 
	@name = name # this.name = name

d = new Dog 'Dowon'
console.log d.name

console.log '----same thing----'

Dog2 = (@name) ->  # 파라미터로 @name을 쓰면 위와 같은 표현임 

d2 = new Dog2 'Dowon'

console.log d2.name 

// 결과 
D:\Development\coffeescript\3>coffee prototype
Dowon
----same thing----
Dowon

// prototype.js 컴파일 내역
(function() {
  var Dog, Dog2, d, d2;
  Dog = function(name) {
    return this.name = name;
  };
  d = new Dog('Dowon');
  console.log(d.name);

  console.log('----same thing----');

  Dog2 = function(name) {
    this.name = name;
  };
  d2 = new Dog2('Dowon');
  console.log(d2.name);
}).call(this);


  - Tip : 만일 (function(){ 코딩내역 }).call(this)  을 없애고 싶다면 --bare 옵션을 사용하면 됨



2) prototype. 표현하기

  - prototype. 을 :: 로 표현한다  

// prototype2.coffee
Dog = (@name) ->

Dog.prototype.growl = ->
	console.log '*growl*'

d = new Dog 'Dowon'
d.growl()

console.log '----same thing----'

Dog2 = (@name) ->
	
Dog2::growl = ->   # :: 로 표현한다 
	console.log '*growl*'

d2 = new Dog2 'Dowon'
d2.growl()

//결과 
D:\Development\coffeescript\3>coffee prototype2
*growl*
----same thing----
*growl*

// 컴파일 내역 : prototype2.js
// Generated by CoffeeScript 1.4.0
(function() {
  var Dog, Dog2, d, d2;
  Dog = function(name) {
    this.name = name;
  };
  Dog.prototype.growl = function() {
    return console.log('*growl*');
  };
  d = new Dog('Dowon');
  d.growl();

  console.log('----same thing----');

  Dog2 = function(name) {
    this.name = name;
  };
  Dog2.prototype.growl = function() {
    return console.log('*growl*');
  };
  d2 = new Dog2('Dowon');
  d2.growl();
}).call(this);


3) 상속에 대한 표현

  - extends 를 통하여 상속받기

// extend.coffee
Dog = (@name) ->

Dog.prototype.growl = ->
	console.log '*growl*'

BorderCollie = (@name, @tricks) ->

BorderCollie extends Dog

BorderCollie::perform = (trick) ->
	if trick in @tricks
		console.log "#{@name} is #{trick}"
	else
		console.log '*whine*'

dowon = new BorderCollie 'Dowon', ['playing dead', 'catching', 'rolling']

dowon.perform 'catching'
dowon.growl()	

// 결과 
D:\Development\coffeescript\3>coffee extend
Dowon is catching
*growl*

// 컴파일 내역 : extend.js
// Generated by CoffeeScript 1.4.0
(function() {
  var BorderCollie, Dog, dowon,
    __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
    __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };

  Dog = function(name) {
    this.name = name;
  };
  Dog.prototype.growl = function() {
    return console.log('*growl*');
  };

  BorderCollie = function(name, tricks) {
    this.name = name;
    this.tricks = tricks;
  };

  __extends(BorderCollie, Dog);

  BorderCollie.prototype.perform = function(trick) {
    if (__indexOf.call(this.tricks, trick) >= 0) {
      return console.log("" + this.name + " is " + trick);
    } else {
      return console.log('*whine*');
    }
  };
  dowon = new BorderCollie('Dowon', ['playing dead', 'catching', 'rolling']);
  dowon.perform('catching');
  dowon.growl();
}).call(this);

 @ :: extends 를 익히자 ^^

coffeescript-3.zip


posted by 윤영식
2013. 1. 24. 21:53 Languages/CoffeeScript

if, switch, for loop 구문등을 사용해 보자 


1) 조건문

  - yes : true, no : false, if not : unless 

  - if not 과 yes no 사용하기 : if2.coffee

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);

// 결과 

D:\Development\coffeescript>coffee switch

1> Here's your code work, dowon

2> Here's your design work, dowon



  - if 문 console.log를 앞으로 뽑아내기 : member.coffee

     + 컴파일 내역

     + 결과는 같지만 구문 코드는 틀리다 

     + 2번과 3번 구문이 제일 짧다 

person1 = 
	name: "dowon"
	relationship: "friend"
person2 = 
	name: "youngsik"
	relationship: "boss"

greet = (person) ->
	if person.relationship is "friend"
		console.log "1> hi, #{person.name}!"
	else if person.relationship is "boss"
		console.log "1> hello, papa!"
greet person1
greet person2
# or
greet2 = (person) ->
	msg = if person.relationship is "friend"
		"2> hi, #{person.name}!"
	else if person.relationship is "boss"
		"2> hello, papa!"
	console.log msg
greet2 person1
greet2 person2
# or
greet3 = (person) ->
	console.log if person.relationship is "friend"
		"3> hi, #{person.name}!"
	else if person.relationship is "boss"
		"3> hello, papa!"
greet3 person1
greet3 person2
# or
greet4 = (person) ->
	msg = switch person.relationship 
		when "friend" then "4> hi, #{person.name}!"
		when "boss" then "4> hello, papa!"
	console.log msg
greet4 person1
greet4 person2

// Generated by CoffeeScript 1.4.0
(function() {
  var greet, greet2, greet3, greet4, person1, person2;
  person1 = {
    name: "dowon",
    relationship: "friend"
  };
  person2 = {
    name: "youngsik",
    relationship: "boss"
  };

  greet = function(person) {
    if (person.relationship === "friend") {
      return console.log("1> hi, " + person.name + "!");
    } else if (person.relationship === "boss") {
      return console.log("1> hello, papa!");
    }
  };
  greet(person1);
  greet(person2);

  greet2 = function(person) {
    var msg;
    msg = person.relationship === "friend" ? "2> hi, " + person.name + "!" : person.relationship === "boss" ? "2> hello, papa!" : void 0;
    return console.log(msg);
  };
  greet2(person1);
  greet2(person2);

  greet3 = function(person) {
    return console.log(person.relationship === "friend" ? "3> hi, " + person.name + "!" : person.relationship === "boss" ? "3> hello, papa!" : void 0);
  };
  greet3(person1);
  greet3(person2);

  greet4 = function(person) {
    var msg;
    msg = (function() {
      switch (person.relationship) {
        case "friend":
          return "4> hi, " + person.name + "!";
        case "boss":
          return "4> hello, papa!";
      }
    })();
    return console.log(msg);
  };
  greet4(person1);
  greet4(person2);
}).call(this);

// 결과 : 전부 동일한 결과를 출력한다 

D:\Development\coffeescript>coffee member

1> hi, dowon!

1> hello, papa!

2> hi, dowon!

2> hello, papa!

3> hi, dowon!

3> hello, papa!

4> hi, dowon!

4> hello, papa!



2) for 구문 

  - for 구문을 다양하게 사용하는 방법

  - 5가지 for loop 문 사용방법 : loop.coffee

arr = ["Net", "Aet", "Photo", "Psd", "Cgt"]

obj = 
	name: "dowon"
	topic: "web development"
	editor: "yun"

###
for ( var i = 0; i < arr.length; i++) {
	console.log(arr[i]);
}	
###
console.log "---1---"
for siteName in arr
	console.log siteName

console.log "---2---"
console.log siteName for siteName in arr

console.log "---3---return array---"
console.log (siteName for siteName in arr)	

console.log "---4---"
for siteName, i in arr
	console.log "#{i}: #{siteName}"	

console.log "----5--"
for siteName, i in arr when siteName.indexOf("P") is 0	
	console.log "#{i}: #{siteName}"	

console.log "---6---"
###
by 2 ===  when i % 2 is 0
###
for siteName, i in arr by 2
	console.log "#{i}: #{siteName}"	


// Generated by CoffeeScript 1.4.0
(function() {
  var arr, i, obj, siteName, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _m, _step;
  arr = ["Net", "Aet", "Photo", "Psd", "Cgt"];

  obj = {
    name: "dowon",
    topic: "web development",
    editor: "yun"
  };

  /*
  for ( var i = 0; i < arr.length; i++) {
  	console.log(arr[i]);
  }
  */
  console.log("---1---");
  for (_i = 0, _len = arr.length; _i < _len; _i++) {
    siteName = arr[_i];
    console.log(siteName);
  }

  console.log("---2---");
  for (_j = 0, _len1 = arr.length; _j < _len1; _j++) {
    siteName = arr[_j];
    console.log(siteName);
  }

  console.log("---3---return array---");
  console.log((function() {
    var _k, _len2, _results;
    _results = [];
    for (_k = 0, _len2 = arr.length; _k < _len2; _k++) {
      siteName = arr[_k];
      _results.push(siteName);
    }
    return _results;
  })());

  console.log("---4---");
  for (i = _k = 0, _len2 = arr.length; _k < _len2; i = ++_k) {
    siteName = arr[i];
    console.log("" + i + ": " + siteName);
  }

  console.log("----5--");
  for (i = _l = 0, _len3 = arr.length; _l < _len3; i = ++_l) {
    siteName = arr[i];
    if (siteName.indexOf("P") === 0) {
      console.log("" + i + ": " + siteName);
    }
  }

  console.log("---6---");
  /*
  by 2 ===  when i % 2 is 0
  */
  for (i = _m = 0, _len4 = arr.length, _step = 2; _m < _len4; i = _m += _step) {
    siteName = arr[i];
    console.log("" + i + ": " + siteName);
  }
}).call(this);	

// 결과

D:\Development\coffeescript\2>coffee loop

---1---

Net

Aet

Photo

Psd

Cgt

---2---

Net

Aet

Photo

Psd

Cgt

---3---return array---

[ 'Net', 'Aet', 'Photo', 'Psd', 'Cgt' ]

---4---

0: Net

1: Aet

2: Photo

3: Psd

4: Cgt

----5--

2: Photo

3: Psd

---6---

0: Net

2: Photo

4: Cgt



3) Scope 알아보기 

  - this 에 대한 유효범위(scope)를 보았을 때 object의 method로 사용되지 않는 단순 function 호출의 this는 global object (즉, UI에서는 window객체)를 가르킨다 (참조)

var classroom = {
	students: ["Jonh", "Jane", "Jill", "Joe"],
	print: function() {
		var thiz = this;
		function getName(i) {
			return thiz.students[i];
		}
		for(var i=0; i < this.students.length; i++) {
			console.log(getName(i));
		}
	}
}
classroom.print();


  - 위의 경우를 coffee로 표현하고자 할 경우 다음과 같이 => 사용하면 "var thiz = this;" 를 표현할 수 있다

classroom = 
	students: ["Jonh", "Jane", "Jill", "Joe"]
	print: ->
		getName = (i) =>    // =>를 사용하면 var thiz = this; 동일 효과가 가능하다 
			this.students[i]

		for s,i in this.students
			console.log getName i 

classroom.print()

// Generated by CoffeeScript 1.4.0
(function() {
  var classroom;
  classroom = {
    students: ["Jonh", "Jane", "Jill", "Joe"],
    print: function() {
      var getName, i, s, _i, _len, _ref, _results,
        _this = this;
      getName = function(i) {
        return _this.students[i];
      };
      _ref = this.students;
      _results = [];
      for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
        s = _ref[i];
        _results.push(console.log(getName(i)));
      }
      return _results;
    }
  };
  classroom.print();
}).call(this);


* 파일 

coffeescript-2.zip


posted by 윤영식
2013. 1. 24. 21:52 Languages/CoffeeScript

CoffeeScript는 ; { 등을 가급적 배제하고 휴먼리더블하게 작성하게 해준다. 한줄띄기, 스페이스, 탭 등으로 구분하여 작문(코딩)을 한다 


1) 기본적인 작성법

  - 문자, 오브젝트, 범위, 변수

  - 문자 다루기 : #{} 을 통하여 변수 맵핑 : hi.coffee

name = "dowon"
greeting = "hi, #{name}!"
multi = """
this is a greeting:
	#{greeting}

"""
console.log multi

// 결과 : """ 를 사용했을 때 내부의 띄워쓰기 그대로 적용됨 <pre> 태그 유사 

D:\Development\coffeescript>coffee hi


this is a greeting:

        hi, dowon!


D:\Development\coffeescript>



  - 범위 다루기 : ..과 ... 의 차이는 끝수 포함/미포함 : range.coffe

range = [0..10]
console.log range

range = [0...10]
console.log range

range = [15...10]
console.log range

// 결과

D:\Development\coffeescript>coffee range

[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

[ 15, 14, 13, 12, 11 ]


  

  - 오브젝트 다루기 : obj.coffee

obj = name: "dowon", job: "Programmer"
console.log obj
obj = 
	name: "dowon-01" 
	job: "Programmer-01"
console.log obj

name = "dowon-02"
job = "Programmer-02"
obj = name: name, job: job
console.log obj

obj = {name, job}
console.log obj

 - key:value 같은 줄에 쓰면 콤마(,) 구분

 - 한줄 띠기 하면 콤마 필요없음

 - {name} 하면 명칭을 그대로 key 명칭을 활용


// 결과 

D:\Development\coffeescript>coffee obj

{ name: 'dowon', job: 'Programmer' }

{ name: 'dowon-01', job: 'Programmer-01' }

{ name: 'dowon-02', job: 'Programmer-02' }

{ name: 'dowon-02', job: 'Programmer-02' }



  - 변수 다루기 : variable.coffee

[one, two, three] = ["1", "2", "3"]
console.log two
console.log three
name = "dowon"
obj = {name, job:"Programm er", other: {name}}
console.log obj

// Generated by CoffeeScript 1.4.0
(function() {
  var name, obj, one, three, two, _ref;
  _ref = ["1", "2", "3"], one = _ref[0], two = _ref[1], three = _ref[2];

  console.log(two);
  console.log(three);
  name = "dowon";
  obj = {
    name: name,
    job: "Programmer",
    other: {
      name: name
    }
  };
  console.log(obj);
}).call(this);

// 결과 

D:\Development\coffeescript>coffee variable

2

3

{ name: 'dowon', job: 'Programmer', other: { name: 'dowon' } }



2) Function 다루기 

  - 주석, 함수, 아규먼트 배열, Math.random 

  - 주석 : # 무시되는 문장 (컴파일시 주석문도 안남음), ### 주석 /* */ 으로 변환 : comment.coffee

one = 1 # comment
###
comment 2
###
two = 2


  - 함수 만들기 : -> 사용 : greet.coffee

###
function greet (name) {
	console.log("hi" + name +"!");
}
greet = function(name) {
}
###

greet = (name) -> console.log "hi #{name}!"
greet "Greeting" 
greet2 = (name = "friend") -> "hi #{name}"
console.log greet2
console.log greet2()

// 결과

D:\Development\coffeescript>coffee greet

hi Greeting!

[Function]

hi friend       <=== () 를 붙여서 함수 호출한다 



  - 배열 아규먼트 만들기 : ... 사용 : array.coffee

test = (x, y, z...) -> 
	console.log x
	console.log y
	console.log z

test "one", "two", "three"
console.log "---------------------"
test "one", "two", "three", "four", "five"	


test2 = (x..., y, z) -> 
	console.log x
	console.log y
	console.log z

test2 "one", "two", "three"
console.log "---------------------"
test2 "one", "two", "three", "four", "five"	

test3 = (x, y..., z) -> 
	console.log x
	console.log y
	console.log z

test3 "one", "two", "three"
console.log "---------------------"
test3 "one", "two", "three", "four", "five"	

// 결과 

D:\Development\coffeescript>coffee array

one

two

[ 'three' ]   

---------------------

one

two

[ 'three', 'four', 'five' ]


// test2 호출 

[ 'one' ]

two

three

---------------------

[ 'one', 'two', 'three' ]

four

five


// test3 호출 

one

[ 'two' ]

three

---------------------

one

[ 'two', 'three', 'four' ]

five



  - 자동 실행 함수 : do 사용하여 자동 실행토록 만들어준다 : anon.coffee

###
(() -> console.log())()
###

###
automatically invoke function
###
do (message = "hi dowon") ->
	console.log message

// Generated by CoffeeScript 1.4.0
/*
(() -> console.log())()
*/

/*
automatically invoke function
*/

(function() {
  (function(message) {
    return console.log(message);
  })("hi dowon");

}).call(this);

// 결과 : 만일 do를 넣지 않으면 수행되지 않음

D:\Development\coffeescript>coffee anon

hi dowon



  - Math.random 사용하기 : random.coffee

###
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);

// 결과

D:\Development\coffeescript>coffee if

true


* 테스트 파일 다운로드



4) CoffeeScript 문법 & 장/단점

  - if 문을 한줄로 사용할 때 조건 뒤에 then 구문 붙이기 

    예) if name is "dowon" then console.log "right"

posted by 윤영식
2013. 1. 24. 21:51 Languages/CoffeeScript

CoffeeScript를 설치하고 사용해 보자 


1. 설치하기

  - Node.js 설치함

  - NPM(Node Package Manager) 설치함 : npm을 통하여 coffe-script 를 설치한다 

  - 참조 : http://coffeescript.org

D:\Development\javascript>npm install -g coffee-script

npm http GET https://registry.npmjs.org/coffee-script

npm http 200 https://registry.npmjs.org/coffee-script

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'

// 결과 

D:\coffeescript>coffee arr.coffee

[ [ '1111', '1111', '1111', '1111' ],

  [ '1111', '1111', '1111', '1111' ],

  [ '1111', '1111', '1111', '1111' ],

  [ '1111', '1111', '1111', '1111' ] ]



- CoffeeScript js를 일반 js 파일로 컴파일 하기 

D:\coffeescript>coffee -c arr.coffee


// 컴파일로 생성된 arr.js 소스 파일 

// Generated by CoffeeScript 1.4.0
(function() {
  var makeArray, myArr;

  makeArray = function(dimension) {
    var arr, i, j, _i, _j;
    arr = [];
    for (i = _i = 0; 0 <= dimension ? _i < dimension : _i > dimension; i = 0 <= dimension ? ++_i : --_i) {
      arr[i] = [];
      for (j = _j = 0; 0 <= dimension ? _j < dimension : _j > dimension; j = 0 <= dimension ? ++_j : --_j) {
        arr[i][j] = '1111';
      }
    }
    return arr;
  };

  myArr = makeArray(4);

  console.log(myArr);

  console.log('dowon hi');

}).call(this);



3. Coffee Watching & REPL(레플) 사용하기

  - coffee 파일이 변경되어 저장되는 시점에 자동으로 컴파일 되도록 한다 : watch

  - w 옵션을 준다 

////////////////////////////////

// watch 옵션 주기

D:\Development\coffeescript>coffee -cw arr.coffee

17:51:59 - compiled arr.coffee

17:52:29 - compiled arr.coffee   <--- arr.coffee 파일 마지막에 새로운 내용을 넣고 저장하면 자동으로 compile 됨


////////////////////////////////

// REPL을 통하여 coffee 수행해 보기

D:\Development\coffeescript>coffee

coffee> 1 + 1

2

coffee> arr = []

[]

coffee> arr[0] = 'dowon'

'dowon'

coffee> arr

[ 'dowon' ]

coffee> arr.push 'hi'

2

coffee> arr

[ 'dowon', 'hi' ]

coffee> arr[1]

'hi'

coffee>



4. Single Page Application (SPA : 스파) 를 만들때 CoffeeScript를 사용하자

  - 코딩을 간결하게 해준다

  - global variable등의 사소한 문법오류를 잡아준다 

  - 그러나 compile된 코드는 좀 긴것 같아서 byte 수의 증가를 초래하는 듯하다

  - BackboneJS와도 잘 어울린다 


<참조>

posted by 윤영식
prev 1 next