Language/JavaScript

[JS] ๐Ÿ“š ES6 ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด ์‚ฌ์šฉ๋ฒ• ์ •๋ฆฌ

์ธํŒŒ_ 2021. 10. 5. 03:57

ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด

Template literals

ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด์ด๋ž€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜๋Š” ์„ ์ง„์ ์ธ ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด์€ ํ‘œํ˜„์‹/๋ฌธ์ž์—ด ์‚ฝ์ž…, ์—ฌ๋Ÿฌ ์ค„ ๋ฌธ์ž์—ด, ๋ฌธ์ž์—ด ํ˜•์‹ํ™”, ๋ฌธ์ž์—ด ํƒœ๊น… ๋“ฑ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.


ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด ๊ธฐ๋ณธ ๋ฌธ๋ฒ•

`string text` // ๋ฌธ์ž์—ด ํ‘œํ˜„
`string text line 1
 string text line 2` // ๊ฐœํ–‰๋œ ๋ฌธ์ž์—ด ํ‘œํ˜„

var expression;
`string text ${expression} string text` // ๋ณ€์ˆ˜๊ฐ’ ๋ฌธ์ž์—ด ์กฐํ•ฉ

function tag() { };
tag `string text ${expression} string text` // ํ•จ์ˆ˜ ํ˜ธ์ถœ ์•„๊ทœ๋จผํŠธ

ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด์€ ์ž‘์€๋”ฐ์˜ดํ‘œ(')๋‚˜ ํฐ๋”ฐ์˜ดํ‘œ(") ๋Œ€์‹  ๋ฐฑํ‹ฑ(`)(grave accent)๋กœ ๊ฐ์‹ธ์ค๋‹ˆ๋‹ค.


Expression interpolation (ํ‘œํ˜„์‹ ์‚ฝ์ž…๋ฒ•)

ES6 ์ด์ „์—” ํ‘œํ˜„์‹์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ผ๋ฐ˜ ๋ฌธ์ž์—ด ์•ˆ์— ์ง‘์–ด๋„ฃ์—ˆ์Šต๋‹ˆ๋‹ค.

var a = 20;
var b = 8;
var c = "์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ";
var str = "์ €๋Š” " + (a + b) + "์‚ด์ด๊ณ  " + c + "๋ฅผ ์ข‹์•„ํ•ฉ๋‹ˆ๋‹ค.";
console.log(str);   //์ €๋Š” 28์‚ด์ด๊ณ  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ข‹์•„ํ•ฉ๋‹ˆ๋‹ค.

 

ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด $์™€ ์ค‘๊ด„ํ˜ธ{}๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‘œํ˜„์‹์„ ํ‘œ๊ธฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

let a = 20;
let b = 8;
let c = "์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ";
let str = `์ €๋Š” ${a+b}์‚ด์ด๊ณ  ${c}๋ฅผ ์ข‹์•„ํ•ฉ๋‹ˆ๋‹ค.`;
console.log(str);   //์ €๋Š” 28์‚ด์ด๊ณ  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ข‹์•„ํ•ฉ๋‹ˆ๋‹ค.

์œ„ ์ฒ˜๋Ÿผ + ์—ฐ์‚ฐ์ž๋กœ ๋ฌธ์ž์—ด์„ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ๊ฒƒ๋ณด๋‹ค ๊ฐ€๋…์„ฑ์ด ๋” ์ข‹์Šต๋‹ˆ๋‹ค.


Tagged templates

ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด์˜ ๋ฐœ์ „๋œ ํ˜•ํƒœ์˜ ํ•˜๋‚˜๋กœ ํƒœ๊ทธ๋“œ ํ…œํ”Œ๋ฆฟ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด์„ ํ•จ์ˆ˜๋กœ ํŒŒ์‹ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

let person = 'Lee';
let age = 28;

let tag = function(strings, personExp, ageExp) {   
    console.log(strings); // ์ฒซ ์ธ์ˆ˜๋Š” ๋ฐฐ์—ด์ด ๋“ค์–ด์˜ค๊ณ 
    console.log(personExp); // ๋‚˜๋จธ์ง€ ์ธ์ˆ˜๋Š” ${person}๊ฐ’์ด ๋“ค์–ด์˜จ๋‹ค.
    console.log(ageExp); // ๋‚˜๋จธ์ง€ ์ธ์ˆ˜๋Š” ${age}๊ฐ’์ด ๋“ค์–ด์˜จ๋‹ค.
};

let output = tag`that ${person} is a ${age}`;

ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด

 

let myTag = function(strings, a, b, c) {
    console.log(`๋ฌธ์ž์—ด ๋ฐฐ์—ด ๊ธธ์ด : ${strings.length}`);
    
    for(let i = 0; i < strings.length; i+=1) {
        console.log(`๋ฌธ์ž์—ด ์š”์†Œ [${i}]: ${strings[i]}`);
    }
    console.log(a);
    console.log(b);
    console.log(c);
}

let html = 5;
let css = 3;
let js = 'es10';
let string = myTag`HTML${html}, CSS${css}, JavaScript ${js}`;

ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด

 

Tagged templates๋Š” ๋ฐ์ดํ„ฐ ๋ณ„๋กœ ์ƒํ™ฉ(์กฐ๊ฑด)์ด ๋‹ค๋ฅธ ๊ฒฝ์šฐ ์œ ์šฉํ•˜๊ฒŒ ์“ฐ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const ramenList = [
    {
        brand: '๋†์‹ฌ',
        items: ['์‹ ๋ผ๋ฉด','์งœํŒŒ๊ฒŒํ‹ฐ','์ฐธ์น˜๋งˆ์š”','๋‘ฅ์ง€๋ƒ‰๋ฉด']
    },
    {
        brand: '์‚ผ์–‘',
        items: ['์‚ผ์–‘๋ผ๋ฉด', '๋ถˆ๋‹ญ๋ณถ์Œ๋ฉด']
    },
    {
        brand: '์˜ค๋šœ๊ธฐ',
        itmes: []
    }
];

console.log(`๊ตฌ๋งค๊ฐ€๋Šฅํ•œ ${ramenList[0].brand}์˜ ๋ผ๋ฉด : ${ramenList[0].items}`);
//๊ตฌ๋งค๊ฐ€๋Šฅํ•œ ๋†์‹ฌ์˜ ๋ผ๋ฉด : ์‹ ๋ผ๋ฉด,์งœํŒŒ๊ฒŒํ‹ฐ,์ฐธ์น˜๋งˆ์š”,๋‘ฅ์ง€๋ƒ‰๋ฉด
console.log(`๊ตฌ๋งค๊ฐ€๋Šฅํ•œ ${ramenList[1].brand}์˜ ๋ผ๋ฉด : ${ramenList[1].items}`);
//๊ตฌ๋งค๊ฐ€๋Šฅํ•œ ์‚ผ์–‘์˜ ๋ผ๋ฉด : ์‚ผ์–‘๋ผ๋ฉด,๋ถˆ๋‹ญ๋ณถ์Œ๋ฉด
console.log(`๊ตฌ๋งค๊ฐ€๋Šฅํ•œ ${ramenList[2].brand}์˜ ๋ผ๋ฉด : ${ramenList[2].items}`);
//๊ตฌ๋งค๊ฐ€๋Šฅํ•œ ์˜ค๋šœ๊ธฐ์˜ ๋ผ๋ฉด : undefined

์œ„์™€ ๊ฐ™์ด ramenList ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์˜ค๋Š” ๊ฒฝ์šฐ, ์˜ค๋šœ๊ธฐ์˜ ๋ผ๋ฉด ๋ฐ์ดํ„ฐ๋Š” ์•„์ง ์ถ”๊ฐ€๊ฐ€ ์•ˆ๋˜์–ด ์žˆ๊ณ , ๊ทธ ๊ฒฐ๊ณผ undefined๋ผ๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์™”์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ์— tagged templates๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

function fn(strings, brand, items) {
    if(undefined === items) {
        return brand + "์˜ ๋ผ๋ฉด์€ ์žฌ๊ณ ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค!";
    } else {
        return strings[0] + brand + strings[1] + items;
    }
}

console.log(fn`๊ตฌ๋งค๊ฐ€๋Šฅํ•œ ${ramenList[0].brand}์˜ ๋ผ๋ฉด : ${ramenList[0].items}`);
//๊ตฌ๋งค๊ฐ€๋Šฅํ•œ ๋†์‹ฌ์˜ ๋ผ๋ฉด : ์‹ ๋ผ๋ฉด,์งœํŒŒ๊ฒŒํ‹ฐ,์ฐธ์น˜๋งˆ์š”,๋‘ฅ์ง€๋ƒ‰๋ฉด
console.log(fn`๊ตฌ๋งค๊ฐ€๋Šฅํ•œ ${ramenList[1].brand}์˜ ๋ผ๋ฉด : ${ramenList[1].items}`);
//๊ตฌ๋งค๊ฐ€๋Šฅํ•œ ์‚ผ์–‘์˜ ๋ผ๋ฉด : ์‚ผ์–‘๋ผ๋ฉด,๋ถˆ๋‹ญ๋ณถ์Œ๋ฉด
console.log(fn`๊ตฌ๋งค๊ฐ€๋Šฅํ•œ ${ramenList[2].brand}์˜ ๋ผ๋ฉด : ${ramenList[2].items}`);
//์˜ค๋šœ๊ธฐ์˜ ๋ผ๋ฉด์€ ์žฌ๊ณ ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค!

Multi-line strings

ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด์„ ์‚ฌ์šฉํ•˜๋ฉด ์—ฌ๋Ÿฌ ๊ฐœํ–‰ ์ค„์˜ ๋ฌธ์ž์—ด๋„ ๋‚˜๋ˆ ์„œ ์ž‘์„ฑํ•  ํ•„์š”๊ฐ€ ์—†์ด ํ•œ๋ฒˆ์— ์ž‘์„ฑ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

console.log("string text line 1\n" + "string text line 2");

//ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด
console.log(`string text line 1
string text line 2`);

Nesting templates (์ค‘์ฒฉ ํ…œํ”Œ๋ฆฟ)

ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฒฝ์šฐ๋งˆ๋‹ค ๋‹ค๋ฅธ ๋ฌธ์ž์—ด์„ ๋ณ€์ˆ˜์— ์ €์žฅํ•˜๊ณ  ์‹ถ์„ ๋•Œ, ํ…œํ”Œ๋ฆฟ์„ ์ค‘์ฒฉํ•ด์„œ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋…์„ฑ์ด ๋” ์ข‹์„ ๋•Œ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

//ES5
var classes = 'header'
classes += (isLargeScreen() ? 
            '' : item.isCollapsed ? 
            ' icon-expander' : ' icon-collapser');
//ES6, Not use nesting templates
const classes = `header ${ isLargeScreen() ? '' :
                (item.isCollapesd ? 
                'icon-expander' : 'icon-collapser')}`;
//ES6, Used nesting templates
const classes = `header ${ isLargeScreen() ? '' :
                `icon-${item.isCollapsed ? 
                'expander' : 'collapser'}` }`;

Raw strings (์›๋ž˜ ๋ฌธ์ž์—ด)

Raw string์€ ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž๋ฅผ ํ•ด์„ํ•˜์ง€ ์•Š์€ ์ผ๋ฐ˜ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
String.raw ํƒœ๊ทธํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ…œํ”Œ๋ฆฟ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•œ ๋Œ€๋กœ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

let s = String.raw`xy\n${1+1}z`;
console.log(s);     //xy\n2z

 

ํƒœ๊ทธ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์›๋ž˜์˜ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋ ค๋ฉด ์ฒซ ๋ฒˆ์งธ ์ธ์ž์˜ raw ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

let tag = function(strings) {
    console.log(strings);
    return strings.raw[0];
}

let str = tag`Hello\nWorld.`;
console.log(str);       //Hello\nWorld.

ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด