๊ด€๋ฆฌ ๋ฉ”๋‰ด

Daily Front_Minhhk

[JS] koans ๋ณต์Šต[(์•”๋ฌต,๋ช…์‹œ)ํƒ€์ž…๋ณ€ํ™˜, ํ˜ธ์ด์ŠคํŒ…, ํ™”์‚ดํ‘œํ•จ์ˆ˜, …etc] ๋ณธ๋ฌธ

Code๊ฐœ๋ฐœ์ผ์ง€

[JS] koans ๋ณต์Šต[(์•”๋ฌต,๋ช…์‹œ)ํƒ€์ž…๋ณ€ํ™˜, ํ˜ธ์ด์ŠคํŒ…, ํ™”์‚ดํ‘œํ•จ์ˆ˜, …etc]

Minhhk 2022. 11. 9. 15:30
๐Ÿ˜ ๋ญ”๊ฐ€ ์žฅํ™ฉํ•˜๊ฒŒ ๋‚˜์—ดํ•œ๊ฑฐ ๊ฐ™์€๋ฐ,, ๋‚˜๋ฆ„ ํ—ท๊ฐˆ๋ ธ๋˜ ๊ฒƒ ์œ„์ฃผ๋กœ ์ •๋ฆฌ๋ฅผ ํ•ด๋ณด์•˜๋‹ค!

์ƒ๊ฐ๋ณด๋‹ค ์•Œ๊ณ  ์žˆ๋Š” ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•œ ๊ฒƒ๋“ค์ด ์˜คํžˆ๋ ค ๋“ค์–ด๊ฐ€์„œ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด๋‹ˆ
๋‚˜๋Š” ์ œ๋Œ€๋กœ ์•Œ๊ณ  ์žˆ๋˜ ๊ฒƒ์ด ์•„๋‹ˆ์—ˆ๊ณ , ์•ˆ๋‹ค๊ณ  ์ฐฉ๊ฐํ•˜๊ณ  ๋Œ€์ถฉ ์“ฐ๊ณ  ์žˆ์—ˆ๋‹คใ…‹ใ…‹ใ…‹
์ •์‹  ์ฐจ๋ฆฌ๊ณ ~~ ์ฐจ๊ทผํžˆ ๋‹ค์‹œ ํ•˜๋ฉด ๋˜๋‹ˆ๊นŒ ๋“œใ…กใ…ก๊ฐ€์ž!!

 

์•”๋ฌต์  ํƒ€์ž… ๋ณ€ํ™˜(Implicit coercion) || ํƒ€์ž… ๊ฐ•์ œ ๋ณ€ํ™˜(Type coercion)

let a = 1
let b = true

console.log(a+b) // 2

typeof(a+b) // 'number'

let a = 1
let b = '1'

console.log(a+b) // 11

typeof(a+b) // 'string'

let a = 12
let b = '1'

// + ์ œ์™ธํ•œ -, *, / ๋Š” ๋„˜๋ฒ„๋กœ ์ธ์‹ํ•˜์—ฌ ๊ณ„์‚ฐ -> ๋„์ถœ
console.log(a-b) // 11

typeof(a-b) // 'number'

// ์ŠคํŠธ๋ง์ด๋ฉด ์ŠคํŠธ๋ง ๋ฐ˜ํ™˜
let a = '12'
let b = true

console.log(a+b) // 12true

typeof(a+b) // 'string'

---

let a = true
let b = '12'

console.log(a+b) // true12

typeof(a+b) // 'string'

---
// ์ˆซ์ž๋ฉด ์ˆซ์ž ๋ฐ˜ํ™˜
let a = 12
let b = true

console.log(a+b) // 13

typeof(a+b) // 'number'

์ตœ๋Œ€ํ•œ ๊ฐ™์€ ํƒ€์ž…๋ผ๋ฆฌ ์—ฐ์‚ฐ์„ ํ•˜๊ณ , ์ฆ‰ ์—„๊ฒฉํ•œ ๋™์น˜ ์—ฐ์‚ฐ('===')์„ ์‚ฌ์šฉํ•˜๊ณ , ์กฐ๊ฑด๋ฌธ์— ๋น„๊ต ์—ฐ์‚ฐ์„ ๋ช…์‹œํ•˜๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ์ข‹๋‹ค.

// ๋…ผ๋ฆฌํ•ฉ(||) ์—ฐ์‚ฐ์ž
// ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ฒฐ์ •ํ•œ ์ฒซ๋ฒˆ์งธ ํ”ผ์—ฐ์‚ฐ์ž์˜ ํ‰๊ฐ€ ๊ฒฐ๊ณผ ์ฆ‰, ๋ฌธ์ž์—ด ‘Cat’๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
'Cat' || 'Dog'  // 'Cat'

false || 'Dog'  // 'Dog'
'Cat' || false  // 'Cat'

// ๋…ผ๋ฆฌ๊ณฑ(&&) ์—ฐ์‚ฐ์ž
// ๋…ผ๋ฆฌ๊ณฑ ์—ฐ์‚ฐ์ž &&๋Š” ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ฒฐ์ •ํ•œ ๋‘๋ฒˆ์งธ ํ”ผ์—ฐ์‚ฐ์ž์˜ ํ‰๊ฐ€ ๊ฒฐ๊ณผ ์ฆ‰, ๋ฌธ์ž์—ด ‘Dog’๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
'Cat' && 'Dog'  // Dog

false && 'Dog'  // false
'Cat' && false  // false

๋ช…์‹œ์  ํƒ€์ž… ๋ณ€ํ™˜

  • ์ŠคํŠธ๋ง ๋ณ€ํ™˜
// 1. String ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ new ์—ฐ์‚ฐ์ž ์—†์ด ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•
// ์ˆซ์ž ํƒ€์ž… => ๋ฌธ์ž์—ด ํƒ€์ž…
console.log(String(1));        // "1"
console.log(String(NaN));      // "NaN"
console.log(String(Infinity)); // "Infinity"
// ๋ถˆ๋ฆฌ์–ธ ํƒ€์ž… => ๋ฌธ์ž์—ด ํƒ€์ž…
console.log(String(true));     // "true"
console.log(String(false));    // "false"

// 2. Object.prototype.toString ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•
// ์ˆซ์ž ํƒ€์ž… => ๋ฌธ์ž์—ด ํƒ€์ž…
console.log((1).toString());        // "1"
console.log((NaN).toString());      // "NaN"
console.log((Infinity).toString()); // "Infinity"
// ๋ถˆ๋ฆฌ์–ธ ํƒ€์ž… => ๋ฌธ์ž์—ด ํƒ€์ž…
console.log((true).toString());     // "true"
console.log((false).toString());    // "false"
  • ์ˆซ์žํ˜• ๋ณ€ํ™˜
// 1. Number ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ new ์—ฐ์‚ฐ์ž ์—†์ด ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•
// ๋ฌธ์ž์—ด ํƒ€์ž… => ์ˆซ์ž ํƒ€์ž…
console.log(Number('0'));     // 0
console.log(Number('-1'));    // -1
console.log(Number('10.53')); // 10.53
// ๋ถˆ๋ฆฌ์–ธ ํƒ€์ž… => ์ˆซ์ž ํƒ€์ž…
console.log(Number(true));    // 1
console.log(Number(false));   // 0

// 2. parseInt, parseFloat ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•(๋ฌธ์ž์—ด๋งŒ ๋ณ€ํ™˜ ๊ฐ€๋Šฅ)
// ๋ฌธ์ž์—ด ํƒ€์ž… => ์ˆซ์ž ํƒ€์ž…
console.log(parseInt('0'));       // 0
console.log(parseInt('-1'));      // -1
console.log(parseFloat('10.53')); // 10.53
  • ๋ถˆ๋ฆฌ์–ธํƒ€์ž… ๋ณ€ํ™˜
// 1. Boolean ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ new ์—ฐ์‚ฐ์ž ์—†์ด ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•
// ๋ฌธ์ž์—ด ํƒ€์ž… => ๋ถˆ๋ฆฌ์–ธ ํƒ€์ž…
console.log(Boolean('x'));       // true
console.log(Boolean(''));        // false
console.log(Boolean('false'));   // true
// ์ˆซ์ž ํƒ€์ž… => ๋ถˆ๋ฆฌ์–ธ ํƒ€์ž…
console.log(Boolean(0));         // false
console.log(Boolean(1));         // true
console.log(Boolean(NaN));       // false
console.log(Boolean(Infinity));  // true
// null ํƒ€์ž… => ๋ถˆ๋ฆฌ์–ธ ํƒ€์ž…
console.log(Boolean(null));      // false
// undefined ํƒ€์ž… => ๋ถˆ๋ฆฌ์–ธ ํƒ€ ์ž…
console.log(Boolean(undefined)); // false
// ๊ฐ์ฒด ํƒ€์ž… => ๋ถˆ๋ฆฌ์–ธ ํƒ€์ž…
console.log(Boolean({}));        // true
console.log(Boolean([]));        // true

ํ˜ธ์ด์ŠคํŒ…์ด๋ž€

ํ˜ธ์ด์ŠคํŒ…(Hoisting)์˜ ๊ฐœ๋… ํ•จ์ˆ˜ ์•ˆ์— ์žˆ๋Š” ์„ ์–ธ๋“ค์„ ๋ชจ๋‘ ๋Œ์–ด์˜ฌ๋ ค์„œ ํ•ด๋‹น ํ•จ์ˆ˜ ์œ ํšจ ๋ฒ”์œ„์˜ ์ตœ์ƒ๋‹จ์— ์„ ์–ธํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.

  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ•จ์ˆ˜๋Š” ์‹คํ–‰๋˜๊ธฐ ์ „์— ํ•จ์ˆ˜ ์•ˆ์— ํ•„์š”ํ•œ ๋ณ€์ˆ˜๊ฐ’๋“ค์„ ๋ชจ๋‘ ๋ชจ์•„์„œ ์œ ํšจ ๋ฒ”์œ„์˜ ์ตœ์ƒ๋‹จ์— ์„ ์–ธํ•œ๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Parser๊ฐ€ ํ•จ์ˆ˜ ์‹คํ–‰ ์ „ ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ ํ›‘๋Š”๋‹ค. ํ•จ์ˆ˜ ์•ˆ์— ์กด์žฌํ•˜๋Š” ๋ณ€์ˆ˜/ํ•จ์ˆ˜์„ ์–ธ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ธฐ์–ตํ•˜๊ณ  ์žˆ๋‹ค๊ฐ€ ์‹คํ–‰์‹œํ‚จ๋‹ค. ์œ ํšจ ๋ฒ”์œ„: ํ•จ์ˆ˜ ๋ธ”๋ก {} ์•ˆ์—์„œ ์œ ํšจ
  • ์ฆ‰, ํ•จ์ˆ˜ ๋‚ด์—์„œ ์•„๋ž˜์ชฝ์— ์กด์žฌํ•˜๋Š” ๋‚ด์šฉ ์ค‘ ํ•„์š”ํ•œ ๊ฐ’๋“ค์„ ๋Œ์–ด์˜ฌ๋ฆฌ๋Š” ๊ฒƒ์ด๋‹ค. ์‹ค์ œ๋กœ ์ฝ”๋“œ๊ฐ€ ๋Œ์–ด์˜ฌ๋ ค์ง€๋Š” ๊ฑด ์•„๋‹ˆ๋ฉฐ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Parser ๋‚ด๋ถ€์ ์œผ๋กœ ๋Œ์–ด์˜ฌ๋ ค์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์‹ค์ œ ๋ฉ”๋ชจ๋ฆฌ์—์„œ๋Š” ๋ณ€ํ™”๊ฐ€ ์—†๋‹ค.

ํ•จ์ˆ˜ ์„ ์–ธ์‹์€ ํ˜ธ์ด์ŠคํŒ…์— ์˜ํ–ฅ์„ ๋ฐ›์ง€๋งŒ, ํ•จ์ˆ˜ ํ‘œํ˜„์‹์€ ํ˜ธ์ด์ŠคํŒ…์— ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค.

// ์‹คํ–‰ ์ „
logMessage();
sumNumbers();

function logMessage() {
  return 'worked';
}

var sumNumbers = function () {
  return 10 + 20;
};
// ์‹คํ–‰ ์‹œ
function logMessage() {
  return 'worked';
}

var sumNumbers;

logMessage(); // 'worked'
sumNumbers(); // Uncaught TypeError: sumNumbers is not a function

sumNumbers = function () {
  return 10 + 20;
};

TIP ํ˜ธ์ด์ŠคํŒ… ์‚ฌ์šฉ ์‹œ ์ฃผ์˜

  • ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ๊ณผ ์œ ์ง€๋ณด์ˆ˜๋ฅผ ์œ„ํ•ด ํ˜ธ์ด์ŠคํŒ…์ด ์ผ์–ด๋‚˜์ง€ ์•Š๋„๋ก ํ•œ๋‹ค. ํ˜ธ์ด์ŠคํŒ…์„ ์ œ๋Œ€๋กœ ๋ชจ๋ฅด๋”๋ผ๋„ ํ•จ์ˆ˜์™€ ๋ณ€์ˆ˜๋ฅผ ๊ฐ€๊ธ‰์  ์ฝ”๋“œ ์ƒ๋‹จ๋ถ€์—์„œ ์„ ์–ธํ•˜๋ฉด, ํ˜ธ์ด์ŠคํŒ…์œผ๋กœ ์ธํ•œ ์Šค์ฝ”ํ”„ ๊ผฌ์ž„ ํ˜„์ƒ์€ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค. let/const๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • var๋ฅผ ์“ฐ๋ฉด ํ˜ผ๋ž€์Šค๋Ÿฝ๊ณ  ์“ธ๋ชจ์—†๋Š” ์ฝ”๋“œ๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿผ ์™œ var์™€ ํ˜ธ์ด์ŠคํŒ…์„ ์ดํ•ดํ•ด์•ผ ํ• ๊นŒ? ES6๋ฅผ ์–ด๋””์—์„œ๋“  ์“ธ ์ˆ˜ ์žˆ์œผ๋ ค๋ฉด ์•„์ง ์‹œ๊ฐ„์ด ๋” ํ•„์š”ํ•˜๋ฏ€๋กœ ES5๋กœ ํŠธ๋žœ์Šค์ปดํŒŒ์ผ์„ ํ•ด์•ผํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์•„์ง์€ var๊ฐ€ ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€ ์ดํ•ดํ•˜๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค.

 โœ… (ํ˜ธ์ด์ŠคํŒ… ํฌ์ŠคํŠธ ์ฐธ๊ณ !) https://gmlwjd9405.github.io/2019/04/22/javascript-hoisting.html

 


๐Ÿ“Œ ๊นŠ์€๋ณต์‚ฌ๋ผ๋Š”๊ฑด ์ฃผ์†Œ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ๊ทธ ๊ฐ’ ์ž์ฒด๋งŒ ๊ฐ€์ ธ์˜ค๋Š”๊ฒƒ์ด๋‹ค.

arr1 = [1,2,3,4]; let arr3 = [...arr1]; ์œผ๋กœ ๋ณต์‚ฌํ•˜๋ฉด?

arr3์„ ๊ฑด๋“œ๋ ค๋„ arr1์— ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค → ์™œ๋ƒํ•˜๋ฉด arr1๊ณผ arr3์˜ ์ฃผ์†Œ๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

์ฃผ์†Œ๊ฐ’์„ ๋Š์–ด์„œ ์š”์†Œ๋งŒ ๋ณต์‚ฌํ•˜๋Š”๊ฑธ ๊นŠ์€๋ณต์‚ฌ

์ฃผ์†Œ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š”๊ฒƒ์„ ์–•์€๋ณต์‚ฌ

Object.assign()
์ „๊ฐœ์—ฐ์‚ฐ์ž ...???

๊ฐ์ฒด์˜ ํ•œ๋‹จ๊ณ„๊นŒ์ง€์˜ ๊นŠ์€๋ณต์‚ฌ๋งŒ ๊ฐ€๋Šฅํ•˜๊ณ ,

๊ทธ ์ด์ƒ์˜ depth๋ถ€ํ„ฐ๋Š” ์—ฌ์ „ํžˆ ์–•์€๋ณต์‚ฌ๊ฐ€ ์ผ์–ด๋‚œ๋‹ค,,


ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ , let func = ( ) ⇒ { }

// ํ•จ์ˆ˜ ํ‘œํ˜„์‹
function () {
    const add = function (x, y) {
        return x + y
}

// ํ™”์‚ดํ‘œ ํ•จ์ˆ˜
const add = (x, y) => {
    return x + y
}

console.log(add(1,2)) // 3

// ๋ฆฌํ„ด์„ ์ƒ๋žต ๊ฐ€๋Šฅ
const subtract = (x, y) => x - y

// ํ•„์š”์— ๋”ฐ๋ผ ์†Œ๊ด„ํ˜ธ๋ฅผ ๋ถ™์ผ ์ˆ˜๋„ ์žˆ๋‹ค.
const multiply = (x, y) => (x * y)

// ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ํ•˜๋‚˜์ผ ๊ฒฝ์šฐ ์†Œ๊ด„ํ˜ธ ์ƒ๋žต์ด ๊ฐ€๋Šฅ!
const divideBy10 = x => x / 10

// 'ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ํด๋กœ์ €๋ฅผ ํ‘œํ˜„
// 1
const adder = x => {
  return y => {
    return x + y
  }
}

// 2
const subtractor = x => y => {
  return x - y
}

์›์‹œ์ž๋ฃŒํ˜•, ์ฐธ์กฐ์ž๋ฃŒํ˜•

let num = 123;
    const msg = "hello";
    let arr = [1, 2, 3];
    const isOdd = true;

  ์›์‹œ ์ž๋ฃŒํ˜•์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜๋Š” ๊ณต๊ฐ„ (stack)
     1 | num |   123
     2 | msg | "hello"
     3 | arr | heap์˜ 12๋ฒˆ๋ถ€ํ„ฐ 3๊ฐœ  // (์‹ค์ œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋Š” ์ฃผ์†Œ)
     4 |isOdd|   true
  =====================================
  Object ์ž๋ฃŒํ˜•์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜๋Š” ๊ณต๊ฐ„ (heap)
     10 ||   
     11 || 
     12 || 1
     13 || 2  
     14 || 3  
  ์‹ค์ œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๋ณ€์ˆ˜๋ฅผ ์œ„์™€ ๊ฐ™์ด ์ €์žฅ

---
  
์›์‹œ ์ž๋ฃŒํ˜•์ด ํ• ๋‹น๋˜๋Š” ๊ฒฝ์šฐ๋Š” ๊ฐ’ ์ž์ฒด๊ฐ€ ํ• ๋‹น๋˜๊ณ , 
์ฐธ์กฐ ์ž๋ฃŒํ˜•์€ ์ฃผ์†Œ๊ฐ€ ํ• ๋‹น๋œ๋‹ค๊ณ  ์•”๊ธฐํ•˜์…”๋„ ์ข‹์Šต๋‹ˆ๋‹ค.
  
const hello = "world"; // "world" ๊ทธ ์ž์ฒด 
const arr = [1, 2, 3]; // [1, 2, 3] ์˜ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ xxxxxx

๋ ‰์‹œ์ปฌ ์Šค์ฝ”ํ”„ (Lexical scope)

  • ๋ณ€์ˆ˜์™€ ํ•จ์ˆ˜๋ฅผ ํ”„๋กœํผํ‹ฐ๋กœ ์ €์žฅํ•˜๋Š” ๊ฐ์ฒด
  • ํ•จ์ˆ˜๋ฅผ ์–ด๋””์„œ ํ˜ธ์ถœํ•˜๋Š”์ง€๊ฐ€ ์•„๋‹ˆ๋ผ ์–ด๋””์— ์„ ์–ธํ•˜์˜€๋Š”์ง€์— ๋”ฐ๋ผ ๊ฒฐ์ •๋˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.
  • ์ฆ‰, ํ•จ์ˆ˜๋ฅผ ์–ด๋””์„œ ์„ ์–ธํ•˜์˜€๋Š”์ง€์— ๋”ฐ๋ผ ์ƒ์œ„ ์Šค์ฝ”ํ”„๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค๋Š” ๋œป์ด๋ฉฐ, ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์ ์€ ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ์ด ์•„๋‹ˆ๋ผ ํ•จ์ˆ˜์˜ ์„ ์–ธ์— ๋”ฐ๋ผ ๊ฒฐ์ •๋œ๋‹ค๋Š” ์ ์ด๋‹ค.
  • ๋‹ค๋ฅธ ๋ง๋กœ, ์ •์  ์Šค์ฝ”ํ”„๋ผ ๋ถ€๋ฅด๊ธฐ๋„ ํ•˜๋‹ค.

https://developer-alle.tistory.com/407 // ์กฐ๊ธˆ ๋” ์ž์„ธํ•œ ์„ค๋ช…ใ„ฑใ„ฑ

ํด๋กœ์ € (closure)

  • ํด๋กœ์ €๋Š” ํ•จ์ˆ˜์™€ ๊ทธ ํ•จ์ˆ˜๊ฐ€ ์„ ์–ธ๋˜์—ˆ์„ ๋•Œ์˜ ๋ ‰์‹œ์ปฌ ํ™˜๊ฒฝ(Lexical environment)๊ณผ์˜ ์กฐํ•ฉ์ด๋ผ๊ณ  ๋ถ€๋ฅด๋ฉฐ, ๋‚ด๋ถ€ ํ•จ์ˆ˜๊ฐ€ ์™ธ๋ถ€(enclosing) ํ•จ์ˆ˜ ๋ณ€์ˆ˜์— ์•ก์„ธ์Šค(์ ‘๊ทผ) ํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ธฐ๋Šฅ์„ ๋งํ•œ๋‹ค.
  • ์™ธ๋ถ€ ํ•จ์ˆ˜ ๋ฐ–์—์„œ ๋‚ด๋ถ€ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋”๋ผ๋„ ์™ธ๋ถ€ ํ•จ์ˆ˜์˜ ์ง€์—ญ ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์ž์‹ ์ด ์ƒ์„ฑ๋  ๋•Œ์˜ ํ™˜๊ฒฝ(Lexical environment)์„ ๊ธฐ์–ตํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ์ด๋Ÿฌํ•œ ํ•จ์ˆ˜๋ฅผ ํด๋กœ์ €(Closure)๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

this

  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ์„ ์–ธํ•  ๋•Œ ๊ฐ’์ด ๊ฒฐ์ •๋˜๋Š” ๊ฒƒ์ด ์žˆ๋Š”๋ฐ ๊ทธ๊ฒƒ์„ ํด๋กœ์ €๋ผ๊ณ  ํ•˜๊ณ , ๋ฐ˜๋ฉด ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์— ์˜ํ•ด์„œ ๊ฐ’์ด ๊ฒฐ์ •๋˜๋Š” ๊ฒƒ์ด ๋ฐ”๋กœ this์ด๋‹ค.
  • this๊ฐ€ ๋ฐ”๋กœ ํ˜ธ์ถœํ•  ๋•Œ ๊ฒฐ์ •๋˜๋Š” ๊ฒƒ์ด๊ณ , ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๊ฐ€ ๋ฐ”๋กœ this์ด๋‹ค.

๐Ÿ‘ฉ๐Ÿป‍๐ŸŽ“ slice() ์—์„œ ์ดˆ๊ณผํ•œ ์ธ๋ฑ์Šค๋ฉด ๋นˆ๋ฐฐ์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค. slice(0);๋ฅผ ํ•˜๋ฉด ๋ณต์‚ฌ๊ฐ€ ๋œ๋‹ค. ⇒ ์™„์ „ ์ƒˆ๋กœ์šด ์ฃผ์†Œ์˜ ๋ฐฐ์—ด์ด ๋‚˜์˜จ๋‹ค.

 

๐Ÿ“Œ const{name} = student ๋Š” const name = student.name ์ด๊ฑด ํ‚ค๊ฐ’ ๊ฐ€์ ธ์˜ค๋Š” ๋ฌธ๋ฒ•์ด๋‹ค

 

โ“ ๊ฐ์ฒด๋Š” ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๋ ค๋ฉด?

⇒ ๊ฐ์ฒด์˜ ํ‚ค์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•ด์•ผํ•จ!

⇒ Object.keys(obj)

⇒ ํ‚ค๊ฐ’์„ ๋ฐฐ์—ด๋กœ ์ถœ๋ ฅ

 

⇒ Object.values(obj)

⇒ ๋ฒจ๋ฅ˜๊ฐ’ ๋ฐฐ์—ด๋กœ ์ถœ๋ ฅ ์œ„ ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•์˜ .length๋ฅผ ๋ถ™์—ฌ์„œ ์ถœ๋ ฅํ•˜๋ฉด ๊ธธ์ด๊ฐ€ ๋‚˜์˜จ๋‹ค

 

Array.from()  ๋ฉ”์„œ๋“œ๋Š” ์œ ์‚ฌ ๋ฐฐ์—ด ๊ฐ์ฒด(array-like object)๋‚˜ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด(iterable object)๋ฅผ ์–•๊ฒŒ ๋ณต์‚ฌํ•ด ์ƒˆ๋กœ์šดArray๊ฐ์ฒด๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

console.log(Array.from('foo'));
// expected output: Array ["f", "o", "o"]

console.log(Array.from([1, 2, 3], x => x + x));
// expected output: Array [2, 4, 6]

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

๊ตฌ์กฐ๋ถ„ํ•ดํ• ๋‹น์ด๋ž€ ๋ฐฐ์—ด์ด๋‚˜ ๊ฐ์ฒด๋ฅผ ํ•ด์ฒดํ•˜์—ฌ ๊ทธ ๊ฐ’์„ ๊ฐœ๋ณ„๋ณ€์ˆ˜์— ๋‹ด์„ ์ˆ˜ ์žˆ๊ฒŒํ•˜๋Š” ํ‘œํ˜„์‹์ด๋‹ค.

๋ฐฐ์—ด

๋ฐฐ์—ด์„ ๋ถ„ํ•ด

const array = ['apple', 'banana', 'orange', 'grapefruit']

const [first, second] = array
console.log(first) // 'apple'
console.log(second) // 'banana'

const result = []
function foo([first, second]) {
  result.push(second)
  result.push(first)
}

foo(array)
console.log(result) //['banana','apple']

์œ„์™€ ๊ฐ™์€ ํ‘œํ˜„์‹์„ ์“ฐ๋ฉด ๋ฐฐ์—ด ์•ˆ์— ์žˆ๋Š” ๊ฐ’๋“ค์„ ๋ณ€์ˆ˜์— ์žฌํ• ๋‹น์ด ๊ฐ€๋Šฅํ•˜๊ณ , ์ด ์žฌํ• ๋‹น์€ ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ๋ฐ›์„ ๋•Œ๋„ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

rest/spread ๋ฌธ๋ฒ•์„ ๋ฐฐ์—ด ๋ถ„ํ•ด์— ์ ์šฉ

const array = ['apple', 'banana', 'orange', 'grapefruit']
const [start, ...rest] = array
console.log(start) //'apple'
console.log(rest) //['banana', 'orange', 'grapefruit']

์œ„์™€ ๊ฐ™์€ ํ‘œํ˜„์‹์œผ๋กœ๋„ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.๋‹จ, ์•„๋ž˜ ๊ฐ™์€ ํ‘œํ˜„์‹์€ ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅ ํ•ฉ๋‹ˆ๋‹ค.

// const [first, ...middle, last] = array

rest๋ฌธ๋ฒ• ๋’ค์—๋Š” ์‰ผํ‘œ๊ฐ€ ์˜ฌ ์ˆ˜ ์—†๋‹ค.

๊ฐ์ฒด

๊ฐ์ฒด์˜ ๋‹จ์ถ•(shorthand) ๋ฌธ๋ฒ•

const name = '๊น€์ฝ”๋”ฉ'
const age = 28

const person = {
  name,
  age,
  level: 'Junior',
}
console.log(person) // {name: '๊น€์ฝ”๋”ฉ', age: 28, level: 'Junior'}

์œ„์™€ ๊ฐ™์ด ๋ณ€์ˆ˜๋ฅผ ๋จผ์ € ์„ ์–ธํ•ด์ค€ ๋’ค, ์„ ์–ธํ•œ ๋ณ€์ˆ˜๋ฅผ ๊ฐ์ฒด์•ˆ์— ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.

๊ฐ์ฒด ๋ถ„ํ•ด

const student = { name: '๊น€์ฝ”๋”ฉ', major: '์ปดํ“จํ„ฐ๊ณตํ•™' }

const { name } = student
// const { name } = student.name
console.log(name) //'๋ฐ•ํ•ด์ปค'

๋ฐฐ์—ด๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ฐ์ฒด์•ˆ์— ์žˆ๋Š” ํ‚ค๋ฅผ ๋ณ€์ˆ˜๋กœ ํ‚ค๊ฐ’์„ ๊ฐ’์œผ๋กœ ๊ฐ€์ง€๊ณ  ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

rest/spread ๋ฌธ๋ฒ•์„ ๊ฐ์ฒด ๋ถ„ํ•ด์— ์ ์šฉ

๋ณ€์ˆ˜๋ช…์ด ํ‚ค๊ฐ’์ผ ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

let itemId = 3
let cartItems = [
    {
      itemId: 1,
      quantity: 1
    },
    {
      itemId: 5,
      quantity: 7
    },
    {
      itemId: 2,
      quantity: 3
    }
  ]
[...cartItems, {itemId, quantity: 1}]
// {itemId: itemId , quantity: 1} ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
 // rest/spread ๋ฌธ๋ฒ•์„ ๊ฐ์ฒด ๋ถ„ํ•ด์— ์ ์šฉ(1)
const student1 = { name: '์ตœ์ดˆ๋ณด', major: '๋ฌผ๋ฆฌํ•™๊ณผ' }
const { name, ...args } = student

console.log(name) //'์ตœ์ดˆ๋ณด'
console.log(args) //{major:'๋ฌผ๋ฆฌํ•™๊ณผ'}

// rest/spread ๋ฌธ๋ฒ•์„ ๊ฐ์ฒด ๋ถ„ํ•ด์— ์ ์šฉ(2)
const student2 = { name: '์ตœ์ดˆ๋ณด', major: '๋ฌผ๋ฆฌํ•™๊ณผ', lesson: '์–‘์ž์—ญํ•™', grade: 'B+' }

function getSummary({ name, lesson: course, grade }) {
  return `${name}๋‹˜์€ ${grade}์˜ ์„ฑ์ ์œผ๋กœ ${course}์„ ์ˆ˜๊ฐ•ํ–ˆ์Šต๋‹ˆ๋‹ค`}

console.log(getSummary(student)) //'์ตœ์ดˆ๋ณด๋‹˜์€ B+์˜ ์„ฑ์ ์œผ๋กœ ์–‘์ž์—ญํ•™์„ ์ˆ˜๊ฐ•ํ–ˆ์Šต๋‹ˆ๋‹ค'

๋ฐฐ์—ด๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.๋˜๋Š” ๊ฐ’์„ ๋ถ„ํ•ดํ•ด์„œ ํ• ๋‹น ํ• ๋•Œ lesson: course๊ฐ™์ด ์„ ์–ธํ•ด ์ฃผ๋ฉด ๋ณ€์ˆ˜๋ช…์ด lesson์ด ์•„๋‹Œ course๋กœ ๋ณ€๊ฒฝ๋˜์–ด ๊ฐ’์ด ํ• ๋‹น ๋œ๋‹ค.

์ด ์™ธ์—๋„ ๋ณ€์ˆ˜๋ช… ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํ• ๋‹น๋œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

// rest/spread ๋ฌธ๋ฒ•์„ ๊ฐ์ฒด ๋ถ„ํ•ด์— ์ ์šฉ(3)
const user = {
  name: '๊น€์ฝ”๋”ฉ',
  company: {
    name: 'Code States',
    department: 'Development',
    role: {
      name: 'Software Engineer'
    }
  },
  age: 35
}

const changedUser = { //์ „๊ฐœ๊ตฌ๋ฌธ์œผ๋กœ ์›๋ณธ์„ ๊ฐ€์ ธ์™€ ์ด๋ฆ„์„ '๋ฐ•ํ•ด์ปค'๋กœ ๋‚˜์ด๋ฅผ '20'์œผ๋กœ ๋ณ€๊ฒฝ
  ...user,
  name: '๋ฐ•ํ•ด์ปค', // : ๋’ค๋Š” ๊ทธ ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค๋Š” ์˜๋ฏธ
  age: 20
}

const overwriteChanges = {
  name: '๋ฐ•ํ•ด์ปค',
  age: 20,
  ...user //๋ณ€์ˆ˜ ์„ ์–ธ ์ดํ›„ ์›๋ณธ์ด ๋ถˆ๋Ÿฌ์™€์ง€๊ธฐ ๋•Œ๋ฌธ์— ์›๋ณธ๊ณผ ๊ฐ™์•„์ง„๋‹ค.
}

const changedDepartment = {
  ...user, // ์ „๊ฐœ๊ตฌ๋ฌธ์„ ํ†ตํ•ด์„œ ์›๋ณธ ใ„ฑใ„ฑ
  company: { // ์›๋ณธ ์•ˆ์— ์žˆ๋Š” company๊ฐ์ฒด์˜ ๊ฐ’์„ ๋ณ€๊ฒฝ
    ...user.company, // ์›๋ณธ์— ์žˆ๋Š” company๊ฐ์ฒด์˜ ๊ฐ’๋“ค์„ ๋ถˆ๋Ÿฌ์˜ด
    department: 'Marketing' // ๊ฐ’์„ ๋ณ€๊ฒฝ
  }
}

console.log(changedUser) /*{
  name: '๋ฐ•ํ•ด์ปค',
  company: {
    name: 'Code States',
    department: 'Development',
    role: {
      name: 'Software Engineer'
    }
  },
  age: 20
} */

console.log(overwriteChanges) /*{
  name: '๊น€์ฝ”๋”ฉ',
  company: {
    name: 'Code States',
    department: 'Development',
    role: {
      name: 'Software Engineer'
    }
  },
  age: 35
}*/

console.log(changedDepartment) /*{
  name: '๊น€์ฝ”๋”ฉ',
  company: {
    name: 'Code States',
    department: 'Marketing',
    role: {
      name: 'Software Engineer'
    }
  },
  age: 35
}*/

 


 

use strict

"use strict"๋Š” ์Šคํฌ๋ฆฝํŠธ ์ตœ์ƒ๋‹จ์ด ์•„๋‹Œ ํ•จ์ˆ˜ ๋ณธ๋ฌธ ๋งจ ์•ž ์— ์˜ฌ ์ˆ˜๋„ ์žˆ๋‹ค.

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์˜ค์ง ํ•ด๋‹น ํ•จ์ˆ˜๋งŒ ์—„๊ฒฉ ๋ชจ๋“œ๋กœ ์‹คํ–‰๋œ๋‹ค.

์—„๊ฒฉ ๋ชจ๋“œ๋Š” ๋Œ€๊ฐœ ์Šคํฌ๋ฆฝํŠธ ์ „์ฒด์— ์ ์šฉํ•œ๋‹ค.

"use strict"๋Š” ๋ฐ˜๋“œ์‹œ ์ตœ์ƒ๋‹จ์— ์œ„์น˜!!!!!!

๋‹ค์Œ ์ฝ”๋“œ์—์„œ๋Š” ์—„๊ฒฉ ๋ชจ๋“œ๊ฐ€ ํ™œ์„ฑํ™”X

alert("some code");
// ํ•˜๋‹จ์— ์œ„์น˜ํ•œ "use strict"๋Š” ์Šคํฌ๋ฆฝํŠธ ์ƒ๋‹จ์— ์œ„์น˜ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๋ฌด์‹œ๋ฉ๋‹ˆ๋‹ค.

"use strict";

// ์—„๊ฒฉ ๋ชจ๋“œ๊ฐ€ ํ™œ์„ฑํ™”๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

"use strict"์˜ ์œ„์—๋Š” ์ฃผ์„๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค → ๋‹ค๋ฅธ๊ฑฐ ์žˆ์œผ๋ฉด ํ™œ์„ฑํ™” X

 


Object.assign()  ๋ฉ”์„œ๋“œ๋Š” ์ถœ์ฒ˜ ๊ฐ์ฒด๋“ค์˜ ๋ชจ๋“  ์—ด๊ฑฐ ๊ฐ€๋Šฅํ•œ ์ž์ฒด ์†์„ฑ์„ ๋ณต์‚ฌํ•ด ๋Œ€์ƒ ๊ฐ์ฒด์— ๋ถ™์—ฌ๋„ฃ์Šต๋‹ˆ๋‹ค.

๊ทธ ํ›„ ๋Œ€์ƒ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };

const returnedTarget = Object.assign(target, source);

console.log(target);
// expected output: Object { a: 1, b: 4, c: 5 }

console.log(returnedTarget === target);
// expected output: true