๐Ÿ’ก Destructuring Assignment (๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น)

๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น์ด๋ž€ ๋ง ์˜๋ฏธ ๊ทธ๋Œ€๋กœ ๊ฐ์ฒด, ๋ฐฐ์—ด์˜ ๊ตฌ์กฐ๋ฅผ ๋ถ„ํ•ดํ•˜์—ฌ ๋ถ„ํ•ดํ•œ ๊ฐ’์„ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

(๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น ๊ตฌ๋ฌธ์€ ๋ฐฐ์—ด์˜ ๊ฐ’์ด๋‚˜ ๊ฐ์ฒด์˜ ์†์„ฑ์„ ๋ณ„๊ฐœ์˜ ๋ณ€์ˆ˜๋กœ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๊ฒŒํ•˜๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์‹)


๐Ÿ’ก ๋ฐฐ์—ด ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น 

let arr = ["dong","hwi"];

// ๋ฐฐ์—ด ๊ตฌ์กฐ ๋ถ„ํ•ด 
let [firstName, surname] = arr;

console.log(firstName);
console.log(surname);

// ๋ฐฐ์—ด์œผ๋กœ ์ ‘๊ทผํ•˜์ง€ ์•Š๊ณ  ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋๋‹ค.

split(' ') ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

let [firstname, surname] = "kim dong".split(' ');

* ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น์„ ํ•œ๋‹ค๊ณ  ํ•ด์„œ ๊ธฐ์กด์˜ arr๋Š” ๋ฐ”๋€Œ์ง€ ์•Š๋Š”๋‹ค.

 

1. ์‰ผํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์š”์†Œ๋ฅผ ๋ฌด์‹œํ•  ์ˆ˜ ์žˆ๋‹ค.

// ๋‘ ๋ฒˆ์งธ ์š”์†Œ๋Š” ํ•„์š”ํ•˜์ง€ ์•Š์Œ
let [firstName, , title] = ["Julius", "Caesar", "Consul", "of the Roman Republic"];

alert( title ); // Consul

2. ํ• ๋‹น ์—ฐ์‚ฐ์ž ์ขŒ์ธก์—” ๋ญ๋“ ์ง€ ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

ํ• ๋‹น ์—ฐ์‚ฐ์ž ์ขŒ์ธก์—” ํ• ๋‹น ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋ผ๋ฉด ์–ด๋–ค ๊ฒƒ์ด๋“ ์ง€ ์˜ฌ์ˆ˜ ์žˆ๋‹ค.(๊ฐ์ฒด ํ”„๋กœํผํ‹ฐ๋„ ๊ฐ€๋Šฅ)

let user = {};
[user.name, user.surname] = "kim hwi".split(' ');

console.log(user.name);

3. entries()๋กœ ๋ฐ˜๋ณตํ• ์ˆ˜ ์žˆ๋‹ค.

let user = {
	name : 'john',
	age:30
};

for(let[key, value] of Object.entries(user)){
	console.log(`${key} : ${value}`);
}

4.'...'๋กœ ๋‚˜๋จธ์ง€ ์š”์†Œ ๋ฐฐ์—ด๋กœ๊ฐ€์ ธ์˜ค๊ธฐ

let [name1, name2, ...rest] = ['a','b','c','d','e'];

console.log(name1);
console.log(name2);

console.log(rest[0]);	// c
console.log(rest[1]);	// d

rest๋ง๊ณ  ๋‹ค๋ฅธ ๋ณ€์ˆ˜๋ช…๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. ํ•˜์ง€๋งŒ ๋ณ€์ˆ˜๋ช… ์•ž์— . ์„ธ๊ฐœ๋ฅผ ๊ผญ ์ง€์ผœ์ค˜์•ผํ•œ๋‹ค.


๐Ÿ’ก ๊ฐ์ฒด ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น

let options = {
	title: "menus",
    width:100,
    height:200
};

let {title, width, height} = options;

console.log(title);
console.log(width);
console.log(height);

์ฐธ๊ณ 

ko.javascript.info/destructuring-assignment

 

๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น

 

ko.javascript.info

 

๐Ÿ’ก Template Literals

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

- ์—ฌ๊ธฐ์„œ ๋ฆฌํ„ฐ๋Ÿด(Literal)์€ ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

- ๊ณ ๋กœ ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด์€ ํ‘œํ˜„์‹์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์ด๋ผ๋Š” ๊ฒƒ์ด๋‹ค.

- ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด์€ ๋ฐฑํ‹ฑ(`` , ์ˆซ์ž 1 ์˜†์— ์žˆ๋‹ค)(grave accent)๊ณผ ${expression}์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. 

 

[ํŠน์ง•]

- ํ•ญ์ƒ ๋ฌธ์ž์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ์ค„๋ฐ”๊ฟˆ ํ•  ๋•Œ \n์„ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด์€ ๊ทธ๋ƒฅ ์—”ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋„์–ด์ฃผ๋ฉด ๋œ๋‹ค.

- ๋ฐฑํ‹ฑ(``)์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ž‘์Œ๋”ฐ์˜ดํ‘œ(''), ํฐ๋”ฐ์˜ดํ‘œ("")๋ฅผ ์ด์Šค์ผ€์ด์Šค๋ฌธ ์—†์ด ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ๋‹ค. 

๐Ÿ’ก ์˜ˆ์‹œ

var a = 5;
var b = 10;
console.log(`Fifteen is ${a + b} and
not ${2 * a + b}.`);
// "Fifteen is 15 and
// not 20."

 

 

 

 

 

์ฐธ๊ณ )

developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Template_literals

 

Template literals - JavaScript | MDN

ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด์€ ๋‚ด์žฅ๋œ ํ‘œํ˜„์‹์„ ํ—ˆ์šฉํ•˜๋Š” ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์ž…๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ์ค„๋กœ ์ด๋ค„์ง„ ๋ฌธ์ž์—ด๊ณผ ๋ฌธ์ž ๋ณด๊ฐ„๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ „ ๋ฒ„์ „์˜ ES2015์‚ฌ์–‘ ๋ช…์„ธ์—์„œ๋Š” "template strings" (ํ…œํ”Œ๋ฆฟ ๋ฌธ

developer.mozilla.org

 

 

+ Recent posts