Javascript ๋ฌธ๋ฒ• again ๐Ÿ“‘

Seoyul Kimยท2020๋…„ 9์›” 1์ผ
0

Javascript

๋ชฉ๋ก ๋ณด๊ธฐ
2/4

๋น„๊ตฌ์กฐํ™” ํ• ๋‹น(๊ตฌ์กฐ๋ถ„ํ•ด) ๋ฌธ๋ฒ•

  • ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ์ฒด ์•ˆ์— ์žˆ๋Š” ๊ฐ’์„ ์ถ”์ถœํ•ด์„œ ๋ณ€์ˆ˜ ํ˜น์€ ์ƒ์ˆ˜๋กœ ๋ฐ”๋กœ ์„ ์–ธํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.
const object = { a: 1, b: 2 };

const { a, b } = object;

console.log(a); // 1
console.log(b); // 2


const object = { a: 1, b: 2 };

function print({ a, b }) {
  console.log(a);
  console.log(b);
}

print(object);
const ironMan = {
  name: 'ํ† ๋‹ˆ ์Šคํƒ€ํฌ',
  actor: '๋กœ๋ฒ„ํŠธ ๋‹ค์šฐ๋‹ˆ ์ฃผ๋‹ˆ์–ด',
  alias: '์•„์ด์–ธ๋งจ'
};

const captainAmerica = {
  name: '์Šคํ‹ฐ๋ธ ๋กœ์ €์Šค',
  actor: 'ํฌ๋ฆฌ์Šค ์—๋ฐ˜์Šค',
  alias: '์บกํ‹ด ์•„๋ฉ”๋ฆฌ์นด'
};

function print(hero) {
  //๊ฐ์ฒด์—์„œ ๊ฐ’๋“ค์„ ์ถ”์ถœํ•ด์„œ ์ƒˆ๋กœ์šด ์ƒ์ˆ˜๋กœ ์„ ์–ธํ•ด์ค€๋‹ค
  const { alias, name, actor } = hero;
  const text = `${alias}(${name}) ์—ญํ• ์„ ๋งก์€ ๋ฐฐ์šฐ๋Š” ${actor} ์ž…๋‹ˆ๋‹ค.`;
  console.log(text);
}

print(ironMan);
print(captainAmerica);
  • ๋ฐฐ์—ด์—์„œ๋„ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•œ๋ฐ, ๋ฐฐ์—ด ์•ˆ์— ์žˆ๋Š” ์›์†Œ๋ฅผ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ์ƒˆ๋กœ ์„ ์–ธํ•ด์ฃผ๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ์œ ์šฉํ•˜๋‹ค.
const array = [1, 2];
const [one, two] = array;

console.log(one);
console.log(two);

const array = [1];
const [one, two = 2] = array;

console.log(one);
console.log(two);
  • ๋งŒ์•ฝ ๊ฐ์ฒด์˜ ๊นŠ์ˆ™ํ•œ ๊ณณ์— ๋“ค์–ด์žˆ๋Š” ๊ฐ’์„ ๊บผ๋‚ด๊ณ  ์‹ถ๋‹ค๋ฉด ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น ๋ฌธ๋ฒ•์„ ๋‘๋ฒˆ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
const deepObject = {
  state: {
    information: {
      name: 'velopert',
      languages: ['korean', 'english', 'chinese']
    }
  },
  value: 5
};

const { name, languages } = deepObject.state.information;
const { value } = deepObject;

const extracted = {
  name,
  languages,
  value
};

console.log(extracted); // {name: "velopert", languages: Array[3], value: 5}
  • ์œ„์˜ extracted๋Š” key์˜ ์ด๋ฆ„์œผ๋กœ ์„ ์–ธ๋œ ๊ฐ’์ด ์กด์žฌํ•œ๋‹ค๋ฉด ๋ฐ”๋กœ ๋งค์นญ์‹œ์ผœ์ฃผ๋Š” ๊ฒƒ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜๋ฏธ์ด๋‹ค.
const extracted = {
  name: name,
  languages: languages,
  value: value
}
  • ๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ํ•œ๋ฒˆ์— ๋ชจ๋‘ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.
const deepObject = {
  state: {
    information: {
      name: 'velopert',
      languages: ['korean', 'english', 'chinese']
    }
  },
  value: 5
};

const {
  state: {
    information: { name, languages }
  },
  value
} = deepObject;

const extracted = {
  name,
  languages,
  value
};

console.log(extracted);

spread์™€ rest

spread

  • ์ด ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ์ฒด ํ˜น์€ ๋ฐฐ์—ด์„ ํŽผ์น  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๊ธฐ์กด์˜ ๊ฒƒ์„ ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š๊ณ  ์ƒˆ๋กฑ๋ˆ„ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์“ฐ์ธ๋‹ค.
const slime = {
  name: '์Šฌ๋ผ์ž„'
};

const cuteSlime = {
  ...slime,
  attribute: 'cute'
};

const purpleCuteSlime = {
  ...cuteSlime,
  color: 'purple'
};

const animals = ['๊ฐœ', '๊ณ ์–‘์ด', '์ฐธ์ƒˆ'];
const anotherAnimals = [...animals, '๋น„๋‘˜๊ธฐ'];
const numbers = [1, 2, 3, 4, 5];

const spreadNumbers = [...numbers, 1000, ...numbers];
console.log(spreadNumbers); // [1, 2, 3, 4, 5, 1000, 1, 2, 3, 4, 5]

Rest

  • spread์™€ ๋น„์Šทํ•˜๊ฒŒ ์ƒ๊ฒผ์ง€๋งŒ ์—ญํ™•์€ ๋งค์šฐ ๋‹ค๋ฅด๋ฉฐ ๊ฐ์ฒด, ๋ฐฐ์—ด, ๊ทธ๋ฆฌ๊ณ  ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์—์„œ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
const purpleCuteSlime = {
  name: '์Šฌ๋ผ์ž„',
  attribute: 'cute',
  color: 'purple'
};

//๋ฐฐ์—ด์˜ ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น์„ ํ†ตํ•˜์—ฌ ์›ํ•˜๋Š” ๊ฐ’์„ ๋ฐ–์œผ๋กœ ๊บผ๋‚ด๊ณ  ๋‚˜๋จธ์ง€ ๊ฐ’์„ rest์•ˆ์— ๋„ฃ๋Š”๋‹ค.
//color๋ฅผ ์ œ์™ธํ•˜๊ณ  ๋“ค์–ด์žˆ๋‹ค.
const { color, ...rest } = purpleCuteSlime;


const numbers = [0, 1, 2, 3, 4, 5, 6];

const [one, ...rest] = numbers;
  • ๊ฐ์ฒด์™€ ๋ฐฐ์—ด์—์„œ ์‚ฌ์šฉํ• ๋•Œ๋Š” ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น ๋ฌธ๋ฒ•๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜๊ธฐ๋„ ํ•œ๋‹ค.
function sum(...rest) {
  return rest.reduce((acc, current) => acc + current, 0);
}

const numbers = [1, 2, 3, 4, 5, 6];
const result = sum(...numbers);
console.log(result);

๋‹จ์ถ• ํ‰๊ฐ€ ๋…ผ๋ฆฌ ๊ณ„์‚ฐ๋ฒ•

const dog = {
  name: '๋ฉ๋ฉ์ด'
};

function getName(animal) {
  return animal && animal.name;
}

const name = getName(dog);
console.log(name); // ๋ฉ๋ฉ์ด
  • A && B ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋  ๋•Œ์—๋Š” A ๊ฐ€ Truthy ํ•œ ๊ฐ’์ด๋ผ๋ฉด, B ๊ฐ€ ๊ฒฐ๊ณผ๊ฐ’์ด ๋˜๊ณ  A ๊ฐ€ Falsy ํ•œ ๊ฐ’์ด๋ผ๋ฉด ๊ฒฐ๊ณผ๋Š” A ๊ฐ€ ๋œ๋‹ค.
const namelessDog = {
  name: ''
};

function getName(animal) {
  const name = animal && animal.name || '์ด๋ฆ„์ด ์—†๋Š” ๋™๋ฌผ์ž…๋‹ˆ๋‹ค.'
}

const name = getName(namelessDog);
console.log(name); // ์ด๋ฆ„์ด ์—†๋Š” ๋™๋ฌผ์ž…๋‹ˆ๋‹ค.
  • A || B ๋Š” ๋งŒ์•ฝ A ๊ฐ€ Truthy ํ• ๊ฒฝ์šฐ ๊ฒฐ๊ณผ๋Š” A ๊ฐ€ ๋˜๊ณ  A ๊ฐ€ Falsy ํ•˜๋‹ค๋ฉด ๊ฒฐ๊ณผ๋Š” B ๊ฐ€ ๋œ๋‹ค.

์‚ผํ•ญ ์—ฐ์‚ฐ์ž

  • ์‚ผํ•ญ์—ฐ์‚ฐ์ž์˜ ์‚ฌ์šฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
์กฐ๊ฑด ? true์ผ๋•Œ : false์ผ๋•Œ
const array = [];
let text = array.length === 0 ? '๋ฐฐ์—ด์ด ๋น„์–ด์žˆ์Šต๋‹ˆ๋‹ค' : '๋ฐฐ์—ด์ด ๋น„์–ด์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.';

console.log(text);

Template Literals

  • ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด์€ ์ผ๋ฐ˜ ๋ฌธ์ž์—ด๊ณผ ๋น„์Šทํ•ด ๋ณด์ด์ง€๋งŒ, โ€˜ ๋˜๋Š” โ€œ ๊ฐ™์€ ํ†ต์ƒ์ ์ธ ๋”ฐ์˜ดํ‘œ ๋ฌธ์ž ๋Œ€์‹  ๋ฐฑํ‹ฑ(backtick) ๋ฌธ์ž `๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

  • ์ผ๋ฐ˜์ ์ธ ๋ฌธ์ž์—ด์—์„œ ์ค„๋ฐ”๊ฟˆ์€ ํ—ˆ์šฉ๋˜์ง€ ์•Š์œผ๋ฉฐ ๊ณต๋ฐฑ(white-space)๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐฑ์Šฌ๋ž˜์‹œ()๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ด์Šค์ผ€์ดํ”„ ์‹œํ€€์Šค(Escape Sequence)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ์•ผ ํ•œ๋‹ค. ES6 ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด์€ ์ผ๋ฐ˜์ ์ธ ๋ฌธ์ž์—ด๊ณผ ๋‹ฌ๋ฆฌ ์—ฌ๋Ÿฌ ์ค„์— ๊ฑธ์ณ ๋ฌธ์ž์—ด์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด ๋‚ด์˜ ๋ชจ๋“  white-space๋Š” ์žˆ๋Š” ๊ทธ๋Œ€๋กœ ์ ์šฉ๋œ๋‹ค.

  • ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด์€ + ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ฌธ์ž์—ด ์ธํ„ฐํด๋ ˆ์ด์…˜(String Interpolation)์ด๋ผ ํ•œ๋‹ค.

  • ๋ฌธ์ž์—ด ์ธํ„ฐํด๋ ˆ์ด์…˜์€ ${ โ€ฆ }์œผ๋กœ ํ‘œํ˜„์‹์„ ๊ฐ์‹ธ๋ฉฐ ๋ฌธ์ž์—ด ์ธํ„ฐํด๋ ˆ์ด์…˜ ๋‚ด์˜ ํ‘œํ˜„์‹์€ ๋ฌธ์ž์—ด๋กœ ๊ฐ•์ œ ํƒ€์ž… ๋ณ€ํ™˜๋œ๋‹ค.

Tagged Templates

  • Tagged Template์€ ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด๋กœ ๊ฐ์‹ธ์ง„ ๋‚ด์šฉ๋“ค์„ ์ธ์ž๋กœ ํ•˜์—ฌ ํ•จ์ˆ˜์— ๋Œ€์ž…ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.

  • Tagged Template์€ ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด ์•ž์— ํ•จ์ˆ˜ ์ด๋ฆ„์„ ํ‘œ๊ธฐํ•ด์ฃผ๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค. expression interpolation์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด๋“ค์ด ๋ถ„ํ• ๋˜์–ด ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ํ•จ์ˆ˜์˜ ์ฒซ๋ฒˆ์งธ parameter๋กœ ์ „๋‹ฌ๋˜๊ณ , ๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋ถ€ํ„ฐ๋Š” expression interpolation์— ๋“ค์–ด๊ฐ„ ๊ฐ’์ด ์ „๋‹ฌ๋œ๋‹ค.

const first = 'Ung-mo';
const last = 'Lee';

// ES5: ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ
console.log('My name is ' + first + ' ' + last + '.');
// "My name is Ung-mo Lee."

// ES6: String Interpolation
console.log(`My name is ${first} ${last}.`);
// "My name is Ung-mo Lee."

๋ฒจ๋กœํผํŠธ์™€ ํ•จ๊ป˜ํ•˜๋Š” ๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ
Temaplate Literals
ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด(Template Literal)๊ณผ ์Šคํƒ€์ผ๋“œ ์ปดํฌ๋„ŒํŠธ(styled-component)

0๊ฐœ์˜ ๋Œ“๊ธ€