| 1 | [[파일:external/coffeescript.org/logo.png]] |
|---|
| 1 | 2 | |
|---|
| 3 | [include(틀:다른 뜻1, other1=커피, rd1=커피)] |
|---|
| 4 | |
|---|
| 5 | {{{#!syntax coffeescript |
|---|
| 6 | alert "Hello, World!" # alert 방식 |
|---|
| 7 | document.write "Hello, world!" # 문서에 쓰는 방식. |
|---|
| 8 | console.log "Hello, world!" # 콘솔 방식 |
|---|
| 9 | }}} |
|---|
| 10 | |
|---|
| 11 | == 개요 == |
|---|
| 12 | [[https://coffeescript.org/|공식 사이트]] |
|---|
| 13 | |
|---|
| 14 | [[자바스크립트]]로 컴파일 되는 프로그래밍 언어. [[npm]]의 coffee-script 모듈을 통해 [[Node.js]]에서도 사용할 수 있다. Backbone.js과 [[언더스코어|underscore.js]] 등을 만든 자바스크립트 진영의 유명한 개발자 Jeremy Ashkenas가 개발했다. |
|---|
| 15 | |
|---|
| 16 | == 특징 == |
|---|
| 17 | * Assignment |
|---|
| 18 | {{{#!syntax coffeescript |
|---|
| 19 | number = 42 |
|---|
| 20 | opposite = true |
|---|
| 21 | }}} |
|---|
| 22 | * Conditional Assignment |
|---|
| 23 | {{{#!syntax coffeescript |
|---|
| 24 | number = -42 if opposite |
|---|
| 25 | }}} |
|---|
| 26 | * Function |
|---|
| 27 | coffeescript 내 모든 함수는 아래와 같이 작성된다. {{{function keyword}}}는 더 이상 필요가 없다. |
|---|
| 28 | {{{#!syntax coffeescript |
|---|
| 29 | square = (x) -> x * x |
|---|
| 30 | cube = (x) -> x * square x |
|---|
| 31 | |
|---|
| 32 | sqrt = (x) -> Math.sqrt(x) |
|---|
| 33 | cbrt = (x) -> |
|---|
| 34 | v = x |
|---|
| 35 | for i in [1...32] |
|---|
| 36 | v *= v |
|---|
| 37 | v *= v |
|---|
| 38 | v *= x |
|---|
| 39 | v = sqrt sqrt v |
|---|
| 40 | x = sqrt sqrt x |
|---|
| 41 | sqrt sqrt v |
|---|
| 42 | |
|---|
| 43 | alert cbrt 8 |
|---|
| 44 | alert Math.cbrt 8 |
|---|
| 45 | }}} |
|---|
| 46 | * Object |
|---|
| 47 | YAML와 비슷한 모양을 띄고 있다. 아래와 같이 정의된 모든 코드는 {{{{...}}} Object 로 변환된다. |
|---|
| 48 | {{{#!syntax coffeescript |
|---|
| 49 | some = |
|---|
| 50 | a: 123 |
|---|
| 51 | b: 1232 |
|---|
| 52 | |
|---|
| 53 | # m/s^2 |
|---|
| 54 | gravity = |
|---|
| 55 | sun: 274.0 |
|---|
| 56 | jupiter: 24.79 |
|---|
| 57 | saturn: 10.445 |
|---|
| 58 | uranus: 8.87 |
|---|
| 59 | neptune: 11.15 |
|---|
| 60 | earth: 99.80665 |
|---|
| 61 | venus: 8.872 |
|---|
| 62 | mars: 3.7 |
|---|
| 63 | mercury: 3.7 |
|---|
| 64 | moon: 1.625 |
|---|
| 65 | pluto: 0.61 |
|---|
| 66 | |
|---|
| 67 | math = -> |
|---|
| 68 | square: (x) -> x * x |
|---|
| 69 | tan: (x) -> Math.tan(x) |
|---|
| 70 | csc: (x) -> 1 / Math.sin(x) |
|---|
| 71 | }}} |
|---|
| 72 | * Existential Operator |
|---|
| 73 | {{{#!syntax coffeescript |
|---|
| 74 | alert "왜 불러" if elvis? |
|---|
| 75 | |
|---|
| 76 | if window.addEventListener? |
|---|
| 77 | document.addEventListener("DOMContentLoaded", ->) |
|---|
| 78 | else |
|---|
| 79 | document.attachEvent("onreadystatechange", ->) |
|---|
| 80 | }}} |
|---|
| 81 | * Array comprehension |
|---|
| 82 | {{{#!syntax coffeescript |
|---|
| 83 | |
|---|
| 84 | map = (fn, src) -> |
|---|
| 85 | fn v for v in src |
|---|
| 86 | |
|---|
| 87 | reduce = (fn, src, v) -> |
|---|
| 88 | if !len src |
|---|
| 89 | return v |
|---|
| 90 | if v? |
|---|
| 91 | [v, src...] = src |
|---|
| 92 | v = fn v, w for w in src |
|---|
| 93 | v |
|---|
| 94 | |
|---|
| 95 | filter = (fn, src) -> |
|---|
| 96 | v for v in src when !!fn v |
|---|
| 97 | |
|---|
| 98 | dropwhile = (fn, src) -> |
|---|
| 99 | v for v in src when !fn v |
|---|
| 100 | |
|---|
| 101 | zip = (iters...) -> |
|---|
| 102 | c = Number.MAX_SAFE_INTEGER |
|---|
| 103 | c = u for iter in iters when c > (u = len iter) |
|---|
| 104 | iter[i] for iter in iters for i in [0...c] |
|---|
| 105 | |
|---|
| 106 | len = (x) -> x.length |
|---|
| 107 | |
|---|
| 108 | concepts = [ |
|---|
| 109 | "branch" |
|---|
| 110 | "prediction" |
|---|
| 111 | "speculative" |
|---|
| 112 | "execution" |
|---|
| 113 | ] |
|---|
| 114 | |
|---|
| 115 | alert map len, concepts |
|---|
| 116 | }}} |
|---|
| 117 | |
|---|
| 118 | == 여담 == |
|---|
| 119 | * [[Node.js]]에서 은근히 많이 쓰인다. 이유는 아무래도 [[npm]]의 커피스크립트 모듈 때문인듯.. |
|---|
| 120 | * 자바스크립트에 비해 문법이 간결하여 많은 개발자들의 사랑을 받았지만 서서히 저물고 있는 중이다. [[http://stackoverflow.com/research/developer-survey-2016#technology-trending-tech-on-stack-overflo|2016년 Stack Overflow 개발자 설문]]에서 저물고 있는 기술 3위를 차지했다.[* 참고로 1위는 Windows Phone(...) 2위는 [[하스켈]].] 아무래도 최신 자바스크립트 문법을 지원하지 않고, 경쟁자인 [[TypeScript]]가 잘 나가고 있기 때문인듯 하다. |
|---|
| 121 | |
|---|
| 122 | [[분류:프로그래밍 언어]][[분류:스크립트 언어]] |
|---|