CHAPTER 1. A gentle introduction to ECMASCRIPT 6

Transpilers

ES6๋Š” ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„์— ๋„๋‹ฌํ•ด์™”๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ๊ฒƒ์€ ๋ชจ๋“  ๋ธŒ๋ผ์šฐ์ ธ์—์„œ ์™„์ „ํžˆ ์ง€์›๋˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋ฆฌ๊ณ , ๋ช‡๋ช‡ ๋ธŒ๋ผ์šฐ์ ธ๋“ค์€ ์ด๋Ÿฐ ํŠธ๋ Œ๋“œ์— ๋’ค์ณ์งˆ ๊ฒƒ์ด๋‹ค. (๋งŒ์•ฝ ๋งˆ์ดํฌ๋กœ์†Œํ”„ํŠธ๊ฐ€ ์—ฃ์ง€์—์„œ๋„ ์ž˜ ๋™์ž‘ํ•œ๋‹ค ํ•˜๋”๋ผ๋„). ๋งŒ์•ฝ ๋‚ด๊ฐ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์— ๋Œ€ํ•ด ๊ฑฑ์ •ํ•œ๋‹ค๊ณ  ํ•˜๋ฉด ๋ชจ๋“  ๋ธŒ๋ผ์šฐ์ ธ์—์„œ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ๋ฌด์—‡์ธ๊ฐ€? ๋ผ๋Š” ์ƒ๊ฐ์„ ํ• ์ง€๋„ ๋ชจ๋ฅธ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹น์‹ ์ด ํ•˜๋Š” ์ƒ๊ฐ์ด ๋งž๋‹ค. ๋•Œ๋ฌธ์— ์˜ค๋ž˜๋œ ๋ธŒ๋ผ์šฐ์ ธ๋Š” ๋ฌด์‹œํ•˜๋Š” ์•ฑ ๋“ค์ด ๋งŽ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜, ES 6๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋Š” JS ๊ฐœ๋ฐœ์ž๋“ค์€ ES6 ์•ฑ์œผ๋กœ ๋งŒ๋“ค๊ธฐ๋ฅผ ์›ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ปค๋ฎค๋‹ˆํ‹ฐ๋Š” ๊ทธ์— ๋Œ€ํ•œ ํ•ด๋‹ต์„ ๋ฐœ๊ฒฌํ•˜์˜€๋‹ค. : a transpiler.

Tanspiler๋Š” ๋ชจ๋“  ๋ธŒ๋ผ์šฐ์ ธ์—์„œ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ES6์ฝ”๋“œ๋ฅผ ์ทจํ•ฉํ•˜์—ฌ ES5 ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜ํ•ด์ค€๋‹ค. ๊ทธ๊ฒƒ์€ ์‹ฌ์ง€์–ด ๋ธŒ๋ผ์šฐ์ ธ์—์„œ ES6 ์†Œ์Šค๋ฅผ ์ง์ ‘์ ์œผ๋กœ ๋””๋ฒ„๊ทธ๋ฅผ ํ—ˆ์šฉํ•ด์ฃผ๋Š” ์†Œ์Šค๋งต ํŒŒ์ผ๋„ ๋ณ€ํ™˜ํ•ด์ค€๋‹ค. ์ด ์‹œ์ ์—์„œ, ES6 ์ฝ”๋“œ๋ฅผ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ๋‘ ๊ฐ€์ง€ ๋Œ€์•ˆ์ด ์žˆ๋‹ค.

  • Traceur, a Google project

  • Babeljs, ์ Š์€ ๊ฐœ๋ฐœ์ž๋กœ(๊ธ€์„ ์ž‘์„ฑํ•  ๋‹น์‹œ์— 17์„ธ ์˜€์Œ)๋ถ€ํ„ฐ ์‹œ์ž‘๋œ ํ”„๋กœ์ ํŠธ๋กœ ๋‹ค์–‘ํ•œ ์š”์†Œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

๊ฐ๊ฐ์€ ์žฅ์ ๊ณผ ๋‹จ์ ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, Babeljs๋Š” Traceur์— ๋น„ํ•ด ๊ฐ€๋…์„ฑ ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ Traceur๋Š” ๊ตฌ๊ธ€ ํ”„๋กœ์ ํŠธ๋กœ์จ Angular์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์˜€์„ ๊ฒฝ์šฐ์— ๋”์šฑ ์„ฑ๋Šฅ์ด ์šฐ์ˆ˜ํ•˜๋‹ค. Angular 2 ์†Œ์Šค์ฝ”๋“œ๋Š” TypeScript๋กœ ๋ฐ”๋€Œ๊ธฐ ์ด์ „์—๋Š” Traceur์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž์ฒด์ ์œผ๋กœ ๋ณ€ํ™˜ํ•˜์˜€๋‹ค.

TypeScript๋Š” Microsoft์—์„œ ๊ฐœ๋ฐœ๋œ ์˜คํ”ˆ ์†Œ์Šค ์–ธ์–ด์ด๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ํ™•์žฅ๋œ ๊ธฐ๋Šฅ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์šฐ๋ฆฌ๋Š” ๊ณง ๊ทธ ์„ธ๋ถ€์ ์œผ๋กœ ํŒŒ์•…ํ•ด๋ณผ ๊ฒƒ์ด๋‹ค. ์†”์งํžˆ Babel์€ Traceur๋ณด๋‹ค ๋”์šฑ ์žฅ์ ์ด ๋งŽ๋‹ค. ๊ทธ๋ž˜์„œ ๋‚˜๋Š” Babel์„ ์‚ฌ์šฉํ•˜๋ผ๊ณ  ์ถฉ๊ณ ํ•œ๋‹ค. Babel์€ ์‚ฌ์‹ค์ƒ ํ‘œ์ค€์œผ๋กœ ๋น ๋ฅด๊ฒŒ ํ™•์‚ฐ ๋˜๊ณ  ์žˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋งŒ์•ฝ ๋„ˆ๊ฐ€ ES6๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋ฅผ ์›ํ•œ๋‹ค๋ฉด, ๋˜๋Š” ๋„ˆ์˜ ํ”„๋กœ์ ํŠธ์— ์ ์šฉํ•˜๊ธธ ์›ํ•œ๋‹ค๋ฉด, transpilers๋ฅผ ์‚ดํŽด๋ณด๊ณ  ๋นŒ๋“œ ๊ณผ์ •์ค‘์— ํ•˜๋‚˜๋กœ ์ถ”๊ฐ€ํ•˜๋„๋ก ํ•ด๋ผ.

์ด๊ฒƒ์€ ๋„ˆ์˜ ES6 ์†Œ์Šค ํŒŒ์ผ์„ ๋™์ผํ•œ ES5์ฝ”๋“œ๋กœ ๋ณ€ํ™˜ํ•ด์ค„๊ฒƒ์ด๋‹ค. ์ด๊ฒƒ์€ ๋งค์šฐ ์ž˜ ๋™์ž‘ํ•˜์ง€๋งŒ ์ƒˆ๋กœ์šด ๋ช‡ ๊ฐ€์ง€ ๊ธฐ๋Šฅ๋“ค์€ ES5๋กœ ๋ณ€ํ™˜ํ•˜๋Š”๊ฒŒ ์–ด๋ ต๊ฑฐ๋‚˜ ๋ถˆ๊ฐ€๋Šฅํ• ์ˆ˜๋„ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜, ํ˜„์žฌ ๊ด€์ ์—์„œ๋Š” ์•„๋ฌด ๊ฑฑ์ •์—†์ด ์‚ฌ์šฉํ•˜๊ธฐ์— ์ถฉ๋ถ„ํ•˜๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์šฐ๋ฆฌ๊ฐ€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์•ˆ์—์„œ ํ•  ์ˆ˜ ์žˆ๋Š” ์ด๋Ÿฐ ์ƒˆ๋กœ์šด ๊ฒƒ๋“ค์„ ์‚ดํŽด๋ณด๋„๋ก ํ•ด๋ผ.

Let

๋งŒ์•ฝ ๋‹น์‹ ์ด JS๋ฅผ ์‚ฌ์šฉํ•ด๋ณธ์ ์ด ์žˆ๋‹ค๋ฉด, var ์„ ์–ธ์€ ์†์ž„์ˆ˜๋ผ๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ์žˆ๋‹ค. ๋‹ค์–‘ํ•œ ๋งŽ์€ ์–ธ์–ด์—์„œ, ๋ณ€์ˆ˜๋Š” ์„ ์–ธํ•˜๋Š” ๊ณณ์—์„œ ์ •์˜๋˜์–ด์ง„๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ JS ์ปจ์…‰์—์„œ๋Š” ๋‹น์‹ ์ด ๋‚˜์ค‘์— ํ˜ธ์ถœ์„ ํ•˜์˜€๋”๋ผ๋„ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•œ ํ•จ์ˆ˜์˜ ์ตœ ์ƒ์œ„์—์„œ ์„ ์–ธ๋˜๋Š” โ€œhoisting(๋Œ์–ด์˜ฌ๋ฆฌ๊ธฐ)โ€์œผ๋กœ ๋ถˆ๋ฆฌ์–ด์ง„๋‹ค. ๋งŒ์•ฝ ๋‹น์‹ ์ด if ์˜์—ญ์•ˆ์—์„œ name์„ ํ˜ธ์ถœํ•˜์˜€๋‹ค.

function getPonyFullName(pony) {
    if(pony.isChampion){
        var name = 'Champion ' + pony.name;
    }
    return pony.name;
}

์ด๊ฒƒ์€ ํ•จ์ˆ˜์˜ ์ƒ์œ„์— ์„ ์–ธํ•œ ๊ฒƒ๊ณผ ๋™์ผํ•˜๋‹ค.

function getPonyFullName(pony) {
    var name;
    if(pony.isChampion){
        name = 'Champion ' + pony.name;
        return name;
    }
    // name is still accessible here
    return pony.name;
}

ES6๋Š” ๋ณ€์ˆ˜ ์„ ์–ธ์— ๋Œ€ํ•ด์„œ ๋‹น์‹ ์ด ๊ธฐ๋Œ€ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ํ›จ์”ฌ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” โ€œletโ€ ์ด๋ผ๋Š” ์ƒˆ๋กœ์šด ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•˜๊ณ  ์žˆ๋‹ค

function getPonyFullName(pony) {
    if(pony.isChampion){
        let name = 'Champion ' + pony.name;
        return name;
    }
    // name is still accessible here
    return pony.name;
}

name ๋ณ€์ˆ˜๋Š” ์ง€๊ธˆ์€ ์ž์‹ ์˜ ์˜์—ญ์•ˆ์—์„œ ์ œํ•œ๋˜์–ด ์žˆ๋‹ค. Let์€ ์ง€์†์ ์ธ ์‚ฌ์šฉํ•˜๋Š” ์˜์—ญ์•ˆ์—์„œ var๋ฅผ ๋Œ€์ฒดํ•˜๊ธฐ ์œ„ํ•ด ์†Œ๊ฐœ๋˜์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋„ˆ๋Š” ์˜ค๋ž˜๋œ var ๋‹จ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  let์„ ๋Œ€์ฒดํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋” ์ข‹์€ ๊ฒƒ์€ ๋งŒ์•ฝ ๋„ˆ๊ฐ€ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ๋„ˆ์˜ ์ฝ”๋“œ์—์„œ ์ž˜๋ชป๋œ ์–ด๋– ํ•œ ์ ์„ ์ง€์ ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค.

Constants

์ƒˆ๋กœ์šด ํ‚ค์›Œ๋“œ & ๋ณ€์ˆ˜๋“ค์— ๋Œ€ํ•œ ์ฃผ์ œ๋กœ์จ, ๋˜ ํ•˜๋‚˜์˜ ํฅ๋ฏธ๋กœ์šด ๊ฒƒ์ด ์žˆ๋‹ค. ES6๋Š” constants๋ฅผ ์„ ์–ธํ•˜๊ธฐ ์œ„ํ•ด const๋ฅผ ์†Œ๊ฐœํ•˜๊ณ  ์žˆ๋‹ค. ๋‹น์‹ ์ด const๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ๋œ๋‹ค๋ฉด, ๊ทธ๊ฒƒ์€ ์ดˆ๊ธฐํ™”๋˜์–ด์งˆ ๊ฒƒ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹น์‹ ์€ ํ›„์— ์–ด๋– ํ•œ ๊ฐ’๋„ ํ• ๋‹นํ•˜์ง€ ๋ชปํ•  ๊ฒƒ์ด๋‹ค.

const poniesInRace = 6;
poniesInRace = 7;

Let์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•œ๋‹ค๋ฉด, constants๋Š” ๋ธ”๋ก ๋‹จ๊ณ„์—์„œ ์˜ค์ง ์ •์˜๋˜์–ด ์งˆ๊ฒƒ์ด๊ณ  ์ƒ์œ„์—์„œ ์„ ์–ธ๋˜์–ด ์žˆ์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค. ๋‹น์‹ ์ด ๋†€๋ž„๋งŒํ•œ ์‚ฌ์†Œํ•œ ํ•œ๊ฐ€์ง€๋Š” ๋‹น์‹ ์€ object์™€ ํ•จ๊ป˜ constants๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ๊ณ , ์ถ”ํ›„์—๋Š” object๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

const PONY = {};
PONY.color = 'blue'; //works

๊ทธ๋Ÿฌ๋‚˜ ๋‹น์‹ ์€ ๋‹ค๋ฅธ object๋ฅผ ํ• ๋‹นํ•  ์ˆ˜๋Š” ์—†๋‹ค.

const PONY = {};
PONY = {color = 'blue'}; //SyntaxError

๋ฐฐ์—ด๋“ค๋„ ๊ฐ™์€ ์ด์œ ์ด๋‹ค.

const PONIES = [];
PONIES.push({color : 'blue'}); //works

PONIES = []; //SyntaxError

Creating Objects

์ƒˆ๋กœ์šด ํ‚ค์›Œ๋“œ๋Š” ์•„๋‹ˆ์ง€๋งŒ ES6์ฝ”๋“œ๋ฅผ ์ฝ๊ณ  ์žˆ๋‹ค๋ฉด ๋‹น์‹ ์˜ ์‹œ์„ ์„ ์‚ฌ๋กœ์žก์„ ๊ฒƒ์ด๋‹ค. ์ง€๊ธˆ์€ ๋‹น์‹ ์ด ๋ณ€์ˆ˜๋กœ์จ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋˜์–ด์งˆ ๋งŒํผ Object ์š”์†Œ๋ฅผ ์ƒ์„ฑ ํ•˜๊ธฐ๋ฅผ ์›ํ•  ๊ฒฝ์šฐ์— object๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฐ€๋ฒผ์šด ์š”์•ฝ ์ •๋ฆฌ์ด๋‹ค.

function createPony() {
    const name = 'Rainbow Dash';
    const color = 'blue';
    return { name: name, color: color };
}

๋”์šฑ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

function createPony() {
    const name = 'Rainbow Dash';
    const color = 'blue';
    return { name, color };
}

css ์ค‘์ฒฉ

  • ๋ฏธ๋””์–ด ์ฟผ๋ฆฌ์™€ ๊ฐ™์ด ๊ทœ์น™ ์„ธํŠธ ๋‚ด๋ถ€์— ๋‹ค์‹œ ์„ ์–ธ๋˜๋Š” ๊ตฌ์กฐ์—์„œ๋Š” 1๋ ˆ๋ฒจ ์”ฉ ์ถ”๊ฐ€ํ•˜์—ฌ ๋“ค์—ฌ์“ฐ๊ธฐ ํ•ฉ๋‹ˆ๋‹ค.

@media screen and (min-width: 750px) {
    html, body { font-size: 19px; }
    .measure {
        margin: 5rem;
        padding: 0 3rem;
    }
}

Destructuring assignment

์ด๊ฒƒ์€ object๋‚˜ ๋ฐฐ์—ด๋“ค๋กœ๋ถ€ํ„ฐ ๋ณ€์ˆ˜๋ฅผ ํ• ๋‹นํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ๊ฐ„๋‹จํ•œ ์š”์•ฝ ์ •๋ฆฌ์ด๋‹ค.

In ES5:

function httpOptions = { timeout: 2000, isCache: true };
// later
var httpTimeout = httpOptions.timeout;
var httpCache = httpOptions.isCache;

Now, in ES6, you can do:

const httpOptions = { timeout: 2000, isCache: true };
// later
const{ timeout: httpTimeout, isCache: httpCache} = httpOptions;

๊ทธ๋ฆฌ๊ณ  ๋‹น์‹ ์€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์ด๊ฒƒ์€ Object ๋‚ด๋ถ€์˜ ์š”์†Œ๋ฅผ ํ‚ค ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ์—๋‚˜ ๋ณ€์ˆ˜๋ฅผ ํ• ๋‹นํ•  ๊ฒฝ์šฐ์— ์•ฝ๊ฐ„์˜ ๋ถˆ์•ˆ๊ฐ์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋‹น์‹ ์ด ๊ฐ„๋‹จํ•˜๊ฒŒ ์ž‘์„ฑํ•œ ์š”์†Œ๋ฅผ ๊ฐ™์€ ์ด๋ฆ„์˜ ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•˜๊ธธ ์›ํ•  ๊ฒฝ์šฐ์— ์ด๊ฒƒ์€ ๊ต‰์žฅํ•˜๋‹ค.

const httpOptions = { timeout: 2000, isCache: true };
// later
const{ timeout, isCache} = httpOptions;
// you now have a variable named 'timeout'
// and one named 'isCache' with correct values

๊ต‰์žฅํ•œ ๊ฒƒ์€ ๋‚ด๋ถ€์˜ object๋ฅผ ํฌํ•จํ•œ ๊ฒƒ๋„ ๋˜ํ•œ ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค.

const httpOptions = { timeout: 2000, isCache: true };
// later
const{ timeout, isCache} = httpOptions;
// you now have a variable named 'chortTimeout' with value 1000
// and a variable named 'mediumTimeout' with value 2000

๊ทธ๋ฆฌ๊ณ  ์ด๋Ÿฌํ•œ ๋ฐฐ์—ด๋“ค๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

๋ฌผ๋ก  ๋ฐฐ์—ด ์•ˆ์— ๋ฐฐ์—ด ๋˜ํ•œ object์•ˆ์— ๋ฐฐ์—ด์ด ์žˆ๋Š” ๊ฒƒ ๋“ฑ๋“ฑ ๋„ ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค. ํ•œ๊ฐ€์ง€ ํฅ๋ฏธ๋กœ์šด ๊ฒƒ์€ ๋ณ€์ˆ˜๋“ค์„ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ๋Œ๋ ค์ค„ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. race์•ˆ์— ์œ„์น˜์™€ pony๋ฅผ ๋Œ๋ ค์ฃผ๋Š” randomPonyInRace ํ•จ์ˆ˜๋ฅผ ์ƒ์ƒํ•ด๋ณด์•„๋ผ.

function randomPonyInRace(){
    const pony = { name: 'Rainbow Dash' };
    const position = 2;
    // ...
    return { pony, position };
}

const { position, pony } = randomPonyInRace();

๋ณ€์ˆ˜์˜ ์œ„์น˜์— ํ•ด๋‹นํ•˜๋Š” ํ•จ์ˆ˜์˜ ์˜ํ•ด ๋Œ๋ ค๋ฐ›๋Š” ์œ„์น˜๋ฅผ ํ• ๋‹นํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์€ ์ƒˆ๋กญ๊ฒŒ ์‚ฌ๋ผ์ง„ ๊ธฐ๋Šฅ์ด๋‹ค. ๋งŒ์•ฝ ๋„ˆ๊ฐ€ ์œ„์น˜์— ๋Œ€ํ•ด ์ƒ๊ด€ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ๋„ˆ๋Š” ์ž‘์„ฑํ• ์ˆ˜ ์žˆ๋‹ค.

function randomPonyInRace(){
    const pony = { name: 'Rainbow Dash' };
    const position = 2;
    // ...
    return { pony, position };
}

const { pony } = randomPonyInRace();

๊ทธ๋ฆฌ๊ณ  ๋„ˆ๋Š” ์˜ค์ง pony๋ฅผ ๊ฐ€์งˆ ๊ฒƒ์ด๋‹ค.

Default parameters and values

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ํŠน์ง• ์ค‘ ํ•˜๋‚˜๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ์•„๋ฌด๋ ‡๊ฒŒ๋‚˜ ์‚ฌ์šฉํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜์—ฌ๋„ ํ—ˆ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์ด์—ˆ๋‹ค :

  • ๋งŒ์•ฝ ๋‹น์‹ ์ด ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฐœ์ˆ˜๋ณด๋‹ค ๋” ๋งŽ์€ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋„˜๊ธด๋‹ค๋ฉด, ๋‚˜๋จธ์ง€ ํŒŒ๋ผ๋ฏธํ„ฐ ๋“ค์€ ๋ฌด์‹œ๋˜์–ด ์งˆ ๊ฒƒ์ด๋‹ค. ( ๋‹น์‹ ์€ ํŠน๋ณ„ํ•œ ๋ณ€์ˆ˜๋“ค๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. )

  • ๋งŒ์•ฝ ๋‹น์‹ ์ด ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฐœ์ˆ˜๋ณด๋‹ค ๋” ์ ์€ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋„˜๊ธด๋‹ค๋ฉด, ๋ถ€์กฑํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค์€ undefined๋กœ ํ• ๋‹น๋  ๊ฒƒ์ด๋‹ค.

    ๋‘ ๋ฒˆ์งธ ๊ฒฝ์šฐ๋Š” ์šฐ๋ฆฌ์—๊ฒŒ ๊ฐ€์žฅ ๊ด€๋ จ ์žˆ๋Š” ๊ฒฝ์šฐ์ด๋‹ค. ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์„ ํƒ์ ์ผ ๊ฒฝ์šฐ์— ์šฐ๋ฆฌ๋Š” ์ฃผ๋กœ ์ ์€ ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค์„ ๋„˜๊ธด๋‹ค.

function getPonies(size, page){
    size = size || 10;
    page = page || 1;
    // ...
    server.get(size, page);
}

์„ ํƒ์ ์ธ ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค์€ ์ฃผ๋กœ ๊ธฐ๋ณธ ๊ฐ’๋“ค์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. OR ๊ธฐํ˜ธ๋Š” ๋งŒ์•ฝ ์™ผ์ชฝ์˜ ๋ณ€์ˆ˜๊ฐ€ undefined์ผ ๊ฒฝ์šฐ์— ๊ธฐํ˜ธ์˜ ์˜ค๋ฅธ์ชฝ ๋ณ€์ˆ˜๋ฅผ ๋„˜๊ธธ ๊ฒƒ์ด๋‹ค. ๋งŒ์•ฝ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์ •ํ™•ํ•˜๊ฒŒ ๊ณต๊ธ‰๋˜์ง€ ์•Š๋Š” ๋‹ค๋ฉด ํ•ด๋‹น ๊ฒฝ์šฐ์—๋Š” 0,false,โ€โ€์ด ๋  ๊ฒƒ์ด๋‹ค. ์ด ์†์ž„์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ getPoines ํ•จ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ˜ธ์ถœ๋  ๊ฒƒ์ด๋‹ค.

getPonies(20, 2);
getPonies(); // same as getPonies(10, 1);
getPonies(15); // same as getPonies(15, 1);

์ด๊ฒƒ์€ ์ •ํ™•ํ•˜๊ฒŒ ๋™์ž‘ํ•˜๊ฒ ์ง€๋งŒ ํ•จ์ˆ˜ body๋ฅผ ์ฝ์ง€ ์•Š์€ ์ƒํƒœ์—์„œ ๊ธฐ๋ณธ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์„ ํƒ์ ์ธ ๋ณ€์ˆ˜๋“ค์€ ๋ช…๋ฐฑํ•˜์ง€๋Š” ์•Š์„ ๊ฒƒ์ด๋‹ค. ES6๋Š” ๊ธฐ๋ณธ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๋” ์œ ์šฉํ•œ ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•œ๋‹ค.

function getPonies(size = 10, page = 1){
    // ...
    server.get(size, page);
}

๋งŒ์•ฝ ์™„๋ฒฝํžˆ ์ •์˜๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด size ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” 10์ด ๋  ๊ฒƒ์ด๊ณ  Page parameter๋Š” 1์ด ๋  ๊ฒƒ์ด๋‹ค.

  • NOTE : ์ž‘์€ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค. ๋งŒ์•ฝ 0 ๋˜๋Š” โ€œโ€ ๊ฐ’์ด ์œ ์š”ํ•œ ๊ฐ’๋“ค์ด๋ผ๋ฉด ์ด๊ฒƒ์€ ํ•˜๋‚˜์˜ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ size = size || 10 ์œผ๋กœ ์ž‘์„ฑ๋˜์–ด ์ง€์—ˆ๋“ฏ์ด ๋ณ€๊ฒฝ๋  ๊ฒƒ์ด๋‹ค. ์ด๊ฒƒ์€ size = size === undefined ? 10 : size; ๋กœ ์ •์˜๋œ ๊ฒƒ๊ณผ ๊ฐ™๋‹ค.

    ๊ธฐ๋ณธ ๊ฐ’์€ ํ•จ์ˆ˜ ์‹คํ–‰์ด ๋  ์ˆ˜๋„ ์žˆ๋‹ค.

๋˜๋Š” ๋‹ค๋ฅธ ๋ณ€์ˆ˜๋“ค, ์ „์—ญ ๋ณ€์ˆ˜๋“ค, ๋˜๋Š” ํ•จ์ˆ˜์˜ ๋‹ค๋ฅธ ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค

function getPonies(size = defaultSize(), page = size - 1){
    // if page is not provided, it will be set to the value
    // of the size parameter minus one.
    // ...
    server.get(size, page);
}

๋งŒ์•ฝ ๋‹น์‹ ์ด ์˜ค๋ฅธ์ชฝ ํŒŒ๋ผ๋ฏธํ„ฐ์— ์ ‘๊ทผ ํ•˜๊ธฐ๋ฅผ ์›ํ•œ๋‹ค๋ฉด, ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ํ•ญ์ƒ undefined ์ผ๊ฒƒ์ด๋‹ค.

function getPonies(size = page, page = 1){
    // size will always be undefined, as the page parameter is on its right
    server.get(size, page);
}

ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋Œ€ํ•œ ์›๋ฆฌ๋Š” ๋ณ€์ˆ˜๋“ค์— ์ ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ ๊ฐ์†Œ๋œ ํ• ๋‹น์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด

const { timeout = 1000 } = httpOptions;
// you now have a variable named 'timeout'
// with the value of 'httpOptions.timeout' if it exists
// or 1000 if not

Rest Operator

ES6๋Š” ํ•จ์ˆ˜๋‚ด์—์„œ ๋ณ€์ˆ˜ ์„ ์–ธํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฐฉ์‹์„ ์†Œ๊ฐœํ•œ๋‹ค. ์ด์ „ ๋ถ€๋ถ„์—์„œ๋„ ์–ธ๊ธ‰ํ•˜์˜€๋“ฏ์ด, ๋‹น์‹ ์€ ํ•จ์ˆ˜์— ๊ทธ ์™ธ์— ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋„˜๊ธธ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํŠน๋ณ„ํ•œ ๋ณ€์ˆ˜๋“ค์„ ์‚ฌ์šฉํ•œ ๊ฒƒ๋“ค์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

function addPonies(ponies){
    for (var i=0; i<arguments.length; i++){
        poniesInRace.push(arguments[i]);
    }
}

addPonies('Rainbow Dash', 'Pinkie Pie');

๊ทธ๋Ÿฌ๋‚˜ ์šฐ๋ฆฌ๊ฐ€ ๋‚˜์˜์ง€๋„ ์•Š๊ณ  ๋ช…๋ฐฑํ•˜์ง€๋„ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์„ ๋™์˜ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค. Poines ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ ˆ๋Œ€ ์‚ฌ์šฉ๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์šฐ๋ฆฌ๋Š” ์–ด๋–ป๊ฒŒ ๋ช‡๋ช‡์˜ ponies๋ฅผ ๋„˜๊ฒจ์•ผ ํ• ์ง€ ์•Œ๊ณ  ์žˆ๋Š”๊ฐ€? ES6๋Š” โ€ฆ์„ ์‚ฌ์šฉํ•˜์—ฌ ์šฐ๋ฆฌ์—๊ฒŒ ๋” ๋‚˜์€ ๋ฌธ๋ฒ•์„ ์ œ๊ณตํ•œ๋‹ค.

function addPonies(...ponies){
    for (let pony of ponies){
        poniesInRace.push(pony);
    }
}

Poines๋Š” ์ด์ œ ์šฐ๋ฆฌ๊ฐ€ ๋ฐ˜๋ณตํ• ์ˆ˜ ์žˆ๋Š” ๋ฐฐ์—ด์ด๋‹ค. For โ€ฆ of ๊ตฌ์กฐ๋Š” ๋ฐ˜๋ณต๋ฌธ์„ ์œ„ํ•ด ์‚ฌ์šฉ๋˜์–ด์ ธ์žˆ๋‹ค. ES6์—์„œ ์ƒˆ๋กœ์šด ํŠน์ง•์œผ๋กœ ์ถ”๊ฐ€๋˜์—ˆ๋‹ค. ์ด๊ฒƒ์€ ์ง‘ํ•ฉ ๋ณ€์ˆ˜๋“ค์„ ๋ฐ˜๋ณตํ•˜๋Š” ๊ฒƒ๋„ ํ—ˆ์šฉํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  for โ€ฆ in์œผ๋กœ์จ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์šฐ๋ฆฌ์˜ ์ฝ”๋“œ๊ฐ€ ๋” ์•„๋ฆ„๋‹ต๊ณ  ๋ช…ํ™•ํ•˜๋Š”๊ฒƒ์— ๋™์˜ ํ•˜๋Š”๊ฐ€? ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์ž๋Š” ๊ฐ์†Œํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ• ๋•Œ๋„ ๋™์ž‘ํ•œ๋‹ค.

const [winner, ...losers] = poniesInRace;
// assuming 'poniesInRace' is an array containing serveral ponies
// 'winner' will have the first pony,
// and 'losers' will be an array of the others cones

๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์ž๋Š” ํ™•์‚ฐ ์—ฐ์‚ฌ์ž์™€ ํ˜ผ๋™๋˜์ง€ ์•Š๋Š”๋‹ค. ๋‚˜๋Š” ๋‹น์‹ ์—๊ฒŒ ๊ต‰์žฅํžˆ ๋น„์Šทํ•œ ์ฝ”๋“œ๋ฅผ ๋ณด์—ฌ์ค„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ™•์‚ฐ ์—ฐ์‚ฐ์ž๋Š” ์ • ๋ฐ˜๋Œ€์ด๋‹ค. ์ด๊ฒƒ์€ ๋ฐฐ์—ด์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๊ณ  ๊ฐ€์น˜์žˆ๋Š” ์˜๋ฏธ์žˆ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์•ˆ์—์„œ ๊ทธ๊ฒƒ์„ ํ™•์‚ฐํ•œ๋‹ค. ์ด์˜ˆ์ œ๋Š” ์ตœ์†Œ๊ฐ’, ์ตœ๋Œ€๊ฐ’ ๊ฐ™์€ ํ•จ์ˆ˜๋กœ ์‚ฌ์šฉ๋ ์ˆ˜ ์žˆ๋‹ค. ๋„ˆ๋Š” ์•„๋งˆ๋„ ๋ฐฐ์—ด์„ ํ˜ธ์ถœํ•˜๊ธธ ์›ํ• ์ง€๋„ ๋ชจ๋ฅธ๋‹ค.

const ponyPrices = [12,3,4];
const minPrice = Math.min(...ponyPrices);

Classes

๊ฐ€์žฅ ์ƒ์ง•์ ์ธ ํŠน์ง•์ค‘์— ํ•˜๋‚˜์ด๋ฉฐ ์šฐ๋ฆฌ๋Š” Angular app์„ ์ž‘์„ฑํ•  ๊ฒฝ์šฐ์— ๋Œ€๋‹จํžˆ ์ž์ฃผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค. ES6๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์— Classes๋ฅผ ์†Œ๊ฐœํ•˜๊ณ  ์žˆ๋‹ค. ๋‹น์‹ ์€ ์‰ฝ๊ฒŒ class์™€ ์ƒ์†์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹น์‹ ์€ ํ”„๋กœํ† ํƒ€์ž… ์ƒ์†์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๊ฒƒ์€ ์ดˆ๋ณด์ž์—๊ฒ ์‰ฝ์ง€ ์•Š์€ ์ž‘์—…์ด๋‹ค.

class Pony {
    constructor(color){
        this.color = color;
    }

    toString(){
        return '${this.color} pony';
        // set that? It is another cool feature of ES6, called template literals
        // we'll talk about these quickly!
    }
}

const bluePony = new Pony('blue');
console.log(bluePony.toString()); // blue pony

Class ์„ ์–ธ์€ ํ•จ์ˆ˜ ์„ ์–ธ๊ณผ ๋‹ค๋ฅด๊ฒŒ ๋Œ์–ด๋‹น๊น€์ด ์•„๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋‹น์‹ ์€ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ด์ „์— ์„ ์–ธํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค. ๋‹น์‹ ์€ ํŠน๋ณ„ํ•œ constructor ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•ด์•ผํ• ์ง€ ๋ชจ๋ฅธ๋‹ค. ๊ทธ๊ฒƒ์€ ์ƒˆ๋กœ์šด pony๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•  ๋•Œ new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์‹คํ–‰๋˜์–ด์ง„๋‹ค. Pony๋Š” color๊ฐ€ ํ•„์š”ํ•˜๊ณ  ์šฐ๋ฆฌ๋Š” color๊ฐ€ โ€œblueโ€๋กœ ๊ตฌ์„ฑ๋œ color ๋ฅผ ๊ฐ€์ง„ pony ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ ๋‹ค. ํ•ด๋‹น ํด๋ž˜์Šค๋Š” ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ  toString()์œผ๋กœ ๋˜์–ด ์žˆ๋‹ค.

class Pony {
    static defaultSpeed() {
        return 10;
    }
}

์ •์ ์ธ ํ•จ์ˆ˜๋Š” ํด๋ž˜์Šค์—์„œ ๋ฐ”๋กœ ์‚ฌ์šฉ๋ ์ˆ˜ ์žˆ๋‹ค.

const speed = Pony.defaultSpeed();

๋งŒ์•ฝ ๋‹น์‹ ์ด ์—ฐ์‚ฐ์ž๋กœ ๋ณ€์ˆ˜๋ฅผ ์—ฐ๊ฒฐํ•˜๊ธธ ์›ํ•œ๋‹ค๋ฉด Class๋Š” getter / setter๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.

Class Pony {
    get color() {
        console.log('get color');
        return this._color;
    }

    set color(newColor) {
        console.log('set color ${newColor}');
        this._color = newColor;
    }
}
const pone = new Pony();
pony.color = 'red';
// set color red
console.log(pony.color);
// get color
// red

๋ฌผ๋ก , ๋‹น์‹ ์ด ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋ฉด ๋‹น์‹ ์€ ES6์—์„œ ์ƒ์†์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

class Animal {
    speed(){
        return 10;
    }
}
class Pony extends Animal {

}
const pony = new Pony();
console.log(pony.speed()); // 10, as Pony overrides the parent method

Animal์€ ๊ธฐ๋ณธ ํด๋ž˜์Šค์ด๊ณ , Pony๋Š” ํŒŒ์ƒ๋œ ํด๋ž˜์Šค์ด๋‹ค. ๋ณด์‹œ๋‹ค์‹œํ”ผ, ํŒŒ์ƒ๋œ ํด๋ž˜์Šค๋Š” ๊ธฐ๋ณธ ํด๋ž˜์Šค์˜ ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์ด๊ฒƒ์€ ๋‹ค์‹œ ์žฌ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

class Animal {
    speed(){
        return 10;
    }
}
class Pony extends Animal {
    speed(){
        return super.speed() + 10;
    }
}
const pony = new Pony();
console.log(pony.speed()); // 20, as Pony overrides the parent method

๋ณด์‹œ๋‹ค์‹œํ”ผ, super๋ผ๋Š” ํ‚ค์›Œ๋“œ๋Š” ๊ธฐ๋ณธ ํด๋ž˜์Šค์˜ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์„ ํ—ˆ์šฉํ•œ๋‹ค. Super ํ‚ค์›Œ๋“œ๋Š” ๊ธฐ๋ณธ ํด๋ž˜์Šค constructor ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์œผ๋กœ constructor์•ˆ์—์„œ ์‚ฌ์šฉ๋ ์ˆ˜์žˆ๋‹ค.

class Animal {
    constructor(speed) {
        this.speed = speed;
    }
}
class Pony extends Animal {
    constructor(speed, color){
        super(speed);
        this.color = color;
    }
}
const pny = new Pony(20, 'blue');
console.log(pony.speed); // 20

Reference URL

Last updated