๐Ÿ’ช JavaScript w/ ์ฝ”๋”ฉ์•™๋งˆ (์ค‘๊ธ‰)

241007 ๋‹จ์–ด JavaScriptJavaScript

Preface

๐Ÿ“Œ ๊ฐœ๋ฐœ ๊ณต๋ถ€ 1๊ฐœ์›” ์ฐจ์ธ to-be ๊ฐœ๋ฐœ์ž์˜ ์ž์Šต ๋ธ”๋กœ๊ทธ๐Ÿ™‚๏พ Aug 15 ~ 21, 2021

ํ˜„์žฌ ์ƒํƒœ
์ค‘๊ธ‰๋„ ๋ฌด์‚ฌํžˆ! ์ž˜ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค!


โž• afterthought

ํ•œ ๋ฒˆ ๋‚ ๋ ธ๋‹ค๊ฐ€ ๋‹ค์‹œ ์ ๋Š” ๊ฒƒ์ธ ๋งŒํผ ๋‚ด ๋จธ๋ฆฌ์— ๋” ์ž˜ ๋“ค์–ด์™”๊ธฐ ๋ฐ”๋ž€๋‹ค.
์ž์‹๊ฐ™์€, ์žฌ์‚ฐ๊ฐ™์€ ์š” ๋…€์„

์ค‘๊ธ‰์€ ๋’ค๋กœ ๊ฐˆ ์ˆ˜๋ก ์–ด๋ ต๋‹ค. ํŠนํžˆ 11๊ฐ•๋ถ€ํ„ฐ ์ƒ์†Œํ•˜๊ณ  ์–ด๋ ค์› ๋‹ค.
๋ฐ˜๋ณตํ•ด์„œ ๋ด์•ผ ํ•œ๋‹ค. ๋‹ค๋ฅธ ์‚ฌ๋žŒ ๊ฐ•์˜๋„ ๋ณด์ž!





JavaScript

1. variable, hoisting, TDZ

๋ณ€์ˆ˜์˜ ์ƒ์„ฑ ๊ณผ์ •

  1. ์„ ์–ธ ๋‹จ๊ณ„ (delcaration)
  2. ์ดˆ๊ธฐํ™” ๋‹จ๊ณ„ (initialization): "undefined"๋ฅผ ํ• ๋‹นํ•ด ์ฃผ๋Š” ๋‹จ๊ณ„
  3. ํ• ๋‹น ๋‹จ๊ณ„ (assignment)

var, let, const

  • var
    • ์ƒ์„ฑ ๊ณผ์ •
      • ์„ ์–ธ ๋‹จ๊ณ„์™€ ์ดˆ๊ธฐํ™” ๋‹จ๊ณ„๊ฐ€ ๋™์‹œ์— ์ด๋ฃจ์–ด์ง
      • ์„ ์–ธ์„ ๋จผ์ € ํ•˜๊ณ  ๋‚˜์ค‘์— ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์„ ํ—ˆ์šฉํ•จ (๊ทธ๋ž˜์„œ ๋‚˜์ค‘์— ๊ฐ’์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ)
    • ์„ ์–ธ
      • ํ•œ ๋ฒˆ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋ฅผ ๋‹ค์‹œ ์„ ์–ธํ•  ์ˆ˜ ์žˆ์Œ
    • hoisting
      • hoisting๋จ
      • ์„ ์–ธ๋ฌธ์ด ์ตœ์ƒ์œ„๋กœ ๋Œ์—ฌ์˜ฌ๋ ค์ง„ ๊ฒƒ์ฒ˜๋Ÿผ ๋™์ž‘ํ•˜๊ธฐ ๋–„๋ฌธ์— ์„ ์–ธํ•˜๊ธฐ ์ „์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ
      • ๋‹จ, ์„ ์–ธ์€ hoisting๋˜์ง€๋งŒ ํ• ๋‹น์€ hoisting๋˜์ง€ ์•Š์Œ
    • scope
      • function-scoped์ž„ : ์œ ์ผํ•˜๊ฒŒ ๋ฒ—์–ด๋‚  ์ˆ˜ ์—†๋Š” scope๊ฐ€ ํ•จ์ˆ˜
  • let
    • ์ƒ์„ฑ ๊ณผ์ •
      • ์„ ์–ธ ๋‹จ๊ณ„๋Š” hoisting๋˜์ง€๋งŒ ์ดˆ๊ธฐํ™” ๋‹จ๊ณ„๋Š” ์‹ค์ œ code์— ๋„๋‹ฌํ–ˆ์„ ๋•Œ ์ด๋ฃจ์–ด์ง
      • ์„ ์–ธ์„ ๋จผ์ € ํ•˜๊ณ  ๋‚˜์ค‘์— ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์„ ํ—ˆ์šฉํ•จ (๊ทธ๋ž˜์„œ ๋‚˜์ค‘์— ๊ฐ’์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ)
    • ํ•œ ๋ฒˆ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋ฅผ ๋‹ค์‹œ ์„ ์–ธํ•  ์ˆ˜ ์—†์Œ
    • hoisting
      • hoisting๋จ
      • ํ•˜์ง€๋งŒ TDZ์˜ ์˜ํ–ฅ์„ ๋ฐ›์•„ ํ• ๋‹นํ•˜๊ธฐ ์ „์— ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ
    • scope
      • block-scoped์ž„
        e.g. ํ•จ์ˆ˜, if๋ฌธ, for๋ฌธ, while๋ฌธ, try/catch๋ฌธ ๋“ฑ
  • const
    • ์ƒ์„ฑ ๊ณผ์ •
      • ์„ ์–ธ ๋‹จ๊ณ„, ์ดˆ๊ธฐํ™” ๋‹จ๊ณ„, ํ• ๋‹น ๋‹จ๊ณ„๊ฐ€ ํ•œ ๋ฒˆ์— ์ด๋ฃจ์–ด์ ธ์•ผ ํ•จ
    • ์„ ์–ธ
      • ํ•œ ๋ฒˆ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋ฅผ ๋‹ค์‹œ ์„ ์–ธํ•  ์ˆ˜ ์—†์Œ
    • hoisting
      • hoisting
      • ํ•˜์ง€๋งŒ TDZ์˜ ์˜ํ–ฅ์„ ๋ฐ›์•„ ํ• ๋‹นํ•˜๊ธฐ ์ „์— ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ
    • scope
      • block-scoped์ž„
        e.g. ํ•จ์ˆ˜, if๋ฌธ, for๋ฌธ, while๋ฌธ, try/catch๋ฌธ ๋“ฑ

hoisting

scope ๋‚ด๋ถ€ ์–ด๋””์„œ๋“  ๋ณ€์ˆ˜ ์„ ์–ธ์ด ์ตœ์ƒ์œ„์— ์„ ์–ธ๋œ ๊ฒƒ์ฒ˜๋Ÿผ ํ–‰๋™ํ•œ๋‹ค.
๋ฌธ์ œ๋Š” var, let, const์˜ scope๊ฐ€ ๋‹ค๋ฅด๋‹ค๋Š” ์ ์ด๋‹ค.

TDZ (Temporal Dead Zone)

์ด ์˜์—ญ์— ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.
ํ• ๋‹นํ•˜๊ธฐ ์ „์— ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— code๊ฐ€ ์˜ˆ์ธก์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์ž ์žฌ์ ์ธ bug๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ let๊ณผ const๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.


2. constructor function

ํ•จ์ˆ˜๋ช…์˜ ์ฒซ ๊ธ€์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋งŒ๋“ ๋‹ค.
new ์—ฐ์‚ฐ์ž๋กœ ํ˜ธ์ถœํ•œ๋‹ค.
์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” ๋ถ•์–ด๋นต ํ‹€๊ณผ ๊ฐ™์œผ๋‹ˆ ํ•„์š”ํ•œ ์žฌ๋ฃŒ๋ฅผ ๊ฐ€์ง€๊ณ  ์ฐ์–ด๋‚ด๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.
์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ผ์ผ์ด ๊ฐ์ฒด literal์„ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

function User(name, age){
  // this = {} : new ํ•จ์ˆ˜๋ช…();์ด ์‹คํ–‰๋˜๋ฉด ์ž‘๋™๋˜๋Š” code
  this.name = name;
  this.age = age;
  // return this; : new ํ•จ์ˆ˜๋ช…();์ด ์‹คํ–‰๋˜๋ฉด ์ž‘๋™๋˜๋Š” code
}

new ํ•จ์ˆ˜๋ช…();
function User(name, age){
  this.name = name;
  this.age = age;
  this.sayName = function () {
    console.log(this.name); // ์—ฌ๊ธฐ์—์„œ this๋Š” vip๋ฅผ ๊ฐ€๋ฆฌํ‚ด
  }
}

let vip = new User("Mai", 40);
vip.sayName(); // "Mai"
function Item(title, price) {
  // this = {}
  this.title = title;
  this.price = price;
  this.showPrice = function () {
    console.log(`๊ฐ€๊ฒฉ์€ ${price}์›์ž…๋‹ˆ๋‹ค :)`);
  };
  // return this;
};

const item1 = new Item("๊ณต์ฑ…", 3000);
const item2 = new Item("์šฐ์‚ฐ", 5000);
const item3 = new Item("๊ฐ€๋ฐฉ", 7000);

console.log(item1, item2, item3);
// โฌ‡๏ธ Output
// Item {title: "๊ณต์ฑ…", price: 3000, showPrice: ๐’‡}
// Item {title: "์šฐ์‚ฐ", price: 5000, showPrice: ๐’‡}
// Item {title: "๊ฐ€๋ฐฉ", price: 7000, showPrice: ๐’‡}

item3.showPrice(); // "๊ฐ€๊ฒฉ์€ 7000์›์ž…๋‹ˆ๋‹ค :)"

3. object methods, computed property

methods

  • Object.assign(A, B)
    • A๊ฐ€ ๋นˆ ๊ฐ’์ด๋ผ๋ฉด ('{}') B ๊ฐ์ฒด๋ฅผ A ์ด๋ฆ„์œผ๋กœ ๋ณต์ œ (์ด method๋ฅผ ์“ฐ์ง€ ์•Š๊ณ  ๋‹จ์ˆœํžˆ '='์œผ๋กœ ๋‘ ๊ฐœ๊ฐ€ ๊ฐ™๋‹ค๊ณ  ํ•˜๋Š” ๊ฑด ๊ฐ์ฒด๊ฐ€ ๋ณต์ œ๋œ ๊ฒŒ ์•„๋‹ˆ๋ผ ๋‘˜ ๋‹ค ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋ฅผ ๋ณด๊ณ  ์žˆ๋Š” ๊ฒƒ)
    • A๊ฐ€ ๋นˆ ๊ฐ’์ด ์•„๋‹ˆ๋ฉด์„œ A์™€ B์˜ property๊ฐ€ ๊ฒน์น˜์ง€ ์•Š์œผ๋ฉด ๋ชจ๋“  property๊ฐ€ ๋ณ‘ํ•ฉ, ๊ฒน์น˜๋ฉด A์˜ property๋Š” ์‚ญ์ œ๋˜๊ณ  B์˜ property๋กœ ๋ฎ์–ด์“ฐ๊ธฐ
// ๐ŸŽต
// (1)
const user = {
  name : "Mai",
  age : 30
}

const user2 = Object.assign({}, user); // user ๊ฐ์ฒด๊ฐ€ user2์˜ ์ด๋ฆ„์œผ๋กœ ๋ณต์ œ๋จ

Object.assign({ gender : "female" }, user); // : user ๊ฐ์ฒด์— gender๋ผ๋Š” property๊ฐ€ ์ƒ๊น€

// (2)
Object.assign({ name : "May", user }, user); // user ๊ฐ์ฒด์˜ name property๊ฐ€ ๋ฎ์–ด์“ฐ๊ธฐ ํ•จ (name : "Mai")

// (3)
Object.assign(user, info1, info2); // info1์ด ๋จผ์ € user์™€ ๋ณ‘ํ•ฉํ•˜๊ณ , ์ด๊ฒƒ์ด info2์™€ ๋ณ‘ํ•ฉ๋จ
  • Object.keys() : ๊ฐ์ฒด์˜ key๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜
  • Object.values() : ๊ฐ์ฒด์˜ ๊ฐ’์„ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜
  • Object.entries() : ๊ฐ์ฒด์˜ key์™€ ๊ฐ’์„ ๋ชจ๋‘ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜
  • Object.fromEntries() : key์™€ ๊ฐ’์ด ๋ฐฐ์—ด๋กœ ๋ฌถ์ธ ๋ฐฐ์—ด์„ ๊ฐ์ฒด๋กœ ๋ฐ˜ํ™˜
// ๐ŸŽต ์–ด๋–ค ๊ฒŒ key๊ฐ€ ๋ ์ง€ ๋ชจ๋ฅด๋Š” ์ƒํ™ฉ์—์„œ ๊ฐ์ฒด ๋งŒ๋“ค ๋•Œ ์œ ์šฉํ•œ code
function makeObj(key, val) {
  return {
    [key]: val,
  };
}

const obj = makeObj('๋‚˜์ด', 33);
console.log(obj); // { '๋‚˜์ด': 33 }

computed property

property์— ์—ฐ์‚ฐ๋œ ๊ฐ’์ด ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค.

let a = "age"

const user = {
  name : "Mike",
  [a] : 30 // age : 30
}
const user = {
  [4 + 7]: 'sum',
  ['Hi, ' + 'this is me']: 'Hello',
};

console.log(user); // { '11': 'sum', 'Hi, this is me': 'Hello' }

4. symbol

key๋ฅผ ์ˆซ์ž๋‚˜ boolean์œผ๋กœ ์ ์„ ์ˆ˜ ์žˆ์œผ๋‚˜, ๊ฒฐ๊ตญ ๋ฌธ์žํ˜•์œผ๋กœ ๋ฐ˜ํ™˜๋œ๋‹ค.
๊ทธ๋ž˜์„œ ๋ฌธ์ž๋กœ ํ™œ์šฉํ•˜๋ฉด ๋œ๋‹ค.
์ด ์™ธ์— key๋กœ Symbol()์„ ์“ธ ์ˆ˜๋„ ์žˆ๋‹ค.

  • Symbol()
    • ๊ธฐ์กด๊ณผ ๋‹ฌ๋ฆฌ 'new'๋ฅผ ๋ถ™์ด์ง€ ์•Š์Œ
    • Symbol()์€ ์œ ์ผํ•œ ์‹๋ณ„์ž๋ฅผ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉํ•จ
      ๊ทธ๋ž˜์„œ ์ด๋ฆ„์ด ๊ฐ™๋”๋ผ๋„ ์ผ์น˜์—ฐ์‚ฐ์ž๋‚˜ ๋™๋“ฑ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ™์ง€ ์•Š๋‹ค๊ณ  ๋‚˜์˜ด
      ์ „์ฒด code ์ค‘ ๋”ฑ ํ•˜๋‚˜์ด๊ธฐ ๋•Œ๋ฌธ์— ์œ ์ผ์„ฑ์ด ๋ณด์žฅ๋จ
    • ํŠน์ • ๊ฐœ์ฒด์— ์›๋ณธ data๋Š” ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š๊ณ  ์†์„ฑ์„ ์ถ”๊ฐ€ํ•  ๋•Œ ์‚ฌ์šฉํ•จ (property๋ช…์ด ๊ฒน์น˜๋ฉด ์•ˆ ๋˜๋‹ˆ๊นŒ)
    • ๊ทธ๋Ÿฌ๋‚˜ method๋ฅผ ์“ฐ๋ฉด ๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜๋˜์ง€๋Š” ์•Š์Œ
// ๐ŸŽต
const a = Symbol();
const b = Symbol();

a === b; // false
a == b; // false

// ๐ŸŽต symbol์˜ ์ด๋ฆ„์ด ๊ถ๊ธˆํ•˜๋ฉด description ์‚ฌ์šฉ
const id = symbol("id์ž…๋‹ˆ๋‹ค.");
id.description; // "id์ž…๋‹ˆ๋‹ค."
  • Symbol.for() : ์ „์—ญ symbol
    • ํ•˜๋‚˜์˜ symbol๋งŒ ๋ณด์žฅ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ. (์—†์œผ๋ฉด ๋งŒ๋“ค๊ณ  ์žˆ์œผ๋ฉด ๊ฐ€์ ธ์˜ด)
      Symbol()์€ ๋งค๋ฒˆ ๋‹ค๋ฅธ Symbol()์„ ์ƒ์„ฑํ•˜์ง€๋งŒ, Symbol.for()๋Š” ํ•˜๋‚˜๋ฅผ ์ƒ์„ฑํ•œ ๋’ค key๋ฅผ ํ†ตํ•ด ๊ฐ™์€ symbol์„ ๊ณต์œ ํ•จ
// ๐ŸŽต
const id1 = Symbol.for("id");
const id2 = Symbol.for("id");

id1 === id2; /// true

// ๐ŸŽต symbol์˜ ์ด๋ฆ„์ด ๊ถ๊ธˆํ•˜๋ฉด keyfor ์‚ฌ์šฉ
Symbol.keyFor("id1") // "id"
// ๐ŸŽต ๋ฉ€๋ฆฌ ๋–จ์–ด์ ธ์žˆ๋Š” ๊ฐ์ฒด์— showName์ด๋ผ๋Š” method๊ฐ€ ์žˆ๋Š”์ง€ ๊ณ ๋ฏผํ•˜์ง€ ์•Š์•„๋„ ๋˜๊ณ , ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ๋งŒ๋“  property๋ฅผ ๋ฎ์–ด์“ฐ๊ธฐํ•  ๊ฑฑ์ •๋„ ํ•  ํ•„์š” ์—†์ด ์ž‘์—…ํ•  ์ˆ˜ ์žˆ๋Š” code

// ๋‹ค๋ฅธ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋งŒ๋“ค์–ด ๋†“์€ ๊ฐ์ฒด
const user = {
  name: 'Mai',
  age: 30,
};

// ๋‚ด ์ž‘์—…
// user.showName = function () {}; ์ด๋ ‡๊ฒŒ ํ•˜์ง€ ๋ง๊ณ 
const showName = Symbol('show name');
user[showName] = function () {
  console.log(this.name);
};

// ์‚ฌ์šฉ์ž๊ฐ€ ์ ‘์†ํ•˜๋ฉด ๋ณด๋Š” message
for (let key in user) {
  console.log(`Her ${key} is ${user[key]}.`);
}
// โฌ‡๏ธ Output
// "Her name is Mai."
// "Her age is 30."

// ๋‚ด๊ฐ€ ๊ถ๊ธˆํ•ดํ–ˆ๋˜ showName๋„ ์ ์–ด๋ณด๋ฉด
user[showName](); // "Mai"

5. numbers / math methods

  • toString()
    • () ์•ˆ์— ์•„๋ฌด๊ฒƒ๋„ ์“ฐ์ง€ ์•Š์œผ๋ฉด ์ˆซ์ž๋ฅผ ๋ฌธ์ž๋กœ ๋ฐ”๊พธ์–ด ์คŒ
      () ์•ˆ์— 2๋ฅผ ์“ฐ๋ฉด 2์ง„๋ฒ•์œผ๋กœ, 8์„ ์“ฐ๋ฉด 8์ง„๋ฒ•์œผ๋กœ, 16์„ ์“ฐ๋ฉด 16์ง„๋ฒ•์œผ๋กœ ๋ฐ”๊พธ์–ด ์คŒ
  • Math.PI : 3.141592653589793
  • Math.ceil() : ์˜ฌ๋ฆผ
  • Math.floor() : ๋‚ด๋ฆผ
  • Math.round() : ๋ฐ˜์˜ฌ๋ฆผ
    โ€ป ์†Œ์ˆ˜์  ์ž๋ฆฌ์ˆ˜ ๋Š๊ธฐ
// ๐ŸŽต ์•„๋ž˜์˜ ์ˆซ์ž๋ฅผ ์†Œ์ˆ˜์  ๋‘˜์งธ ์ž๋ฆฌ๊นŒ์ง€ ํ‘œํ˜„ํ•˜๊ธฐ (์…‹์งธ ์ž๋ฆฌ์—์„œ ๋ฐ˜์˜ฌ๋ฆผ)
let userRate = 30.1234;

// (1) 100 ๊ณฑํ•œ ๊ฐ’์— Math.round method๋ฅผ ์“ฐ๊ณ  ๋‹ค์‹œ 100์œผ๋กœ ๋‚˜๋ˆ„๊ธฐ
Math.round(userRate * 100) / 100 // 30.12

// (2) toFixed() ์‚ฌ์šฉ
userRate.toFixed(2); // "30.12"
Number(userRate.toFixed(2)) // 30.12
  • toFixed() : () ์•ˆ์— ์“ด ์ˆซ์ž๊นŒ์ง€ ํ‘œํ˜„ (๋‹จ, ๋ฌธ์ž๋กœ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ ์ฃผ์˜)
let userRate = 30.1234;

userRate.toFixed(0); // "30"
userRate.toFixed(6); // "30.123400"
  • isNaN() : ์ˆซ์ž์ธ์ง€ ์•„๋‹Œ์ง€ ํŒ๋‹จํ•  ๋•Œ ์‚ฌ์šฉํ•จ (๊ทธ๋ƒฅ NaN์ด ์•„๋‹ˆ๋ผ isNaN()์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•จ)
x == NaN // false
x === NaN // false
NaN == NaN // false

isNaN(x) // true
isNan(3) // false
  • parseInt()
    • ๋ฌธ์ž์—ด์„ ์ˆซ์ž๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ๊ฑด Number()์™€ ๋™์ผํ•˜๋‚˜ parseInt()๋Š” ๋ฌธ์ž๋„ ์ผ๋ถ€ ์ฒ˜๋ฆฌํ•จ (Number()์— ๋ฌธ์ž๋ฅผ ๋„ฃ์œผ๋ฉด 'NaN'์ž„) (๋‹จ, parseInt()๋„ ์ˆซ์ž๋กœ ์‹œ์ž‘ํ•˜์ง€ ์•Š์œผ๋ฉด 'NaN'์„ ๋ฐ˜ํ™˜ํ•จ)
    • ์†Œ์ˆ˜์ ์€ ๋ฌด์‹œํ•จ
    • comma ๋’ค์— ๋ช‡ ์ง„์ˆ˜๋กœ ๋ฐ”๊ฟ€์ง€ ์ ์–ด์ฃผ๋ฉด ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•ด๋„ 'NaN'๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์Œ
let margin = "10px";
parseInt(margin); // 10
Number(margin); // NaN

let redColor = "f3";
parseInt(redColor); // NaN
parseInt(redColor, 16); // 243

parseInt(11, 2) // 3
  • parseFloat() : parseInt()์™€ ๊ฑฐ์˜ ๋™์ผํ•˜์ง€๋งŒ ๋ถ€๋™ ์†Œ์ˆ˜์  ์ž๋ฆฌ๋„ ๋ฐ˜ํ™˜ํ•จ
let padding = "18.5%";
parseInt(padding); // 18
parseFloat(padding); // 18.5
  • Math.random() : 0 ~ 1 ๋ฌด์ž‘์œ„ ์ˆซ์ž ์ƒ์„ฑ
// ๐ŸŽต 1 ~ 100 ์ค‘ ์ž„์˜์˜ ์ˆซ์ž ๋ฝ‘๊ธฐ

Math.floor(Math.random() * 100) + 1
// ๐ŸŽต ๋ฌด์ž‘์œ„ ์ˆ˜ ๋ฝ‘๊ธฐ
// ๊ทธ ์ˆ˜์— 100 ๊ณฑํ•˜๊ธฐ (1 ~ 50 ์ค‘ ์ž„์˜์˜ ์ˆซ์ž๋ฅผ ๋ฝ‘๊ณ  ์‹ถ๋‹ค๋ฉด 50 ๊ณฑํ•˜๊ธฐ)
// Math.floor๋กœ ์†Œ์ˆ˜์  ์—†์• ๊ธฐ
// ์ด๋•Œ 0์ด ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ 1 ๋”ํ•˜๊ธฐ

๐Ÿง ๊ทธ๋Ÿผ Math.ceil()์„ ์จ์„œ ๋์„ '+1'์ด ์•„๋‹Œ '-1'์„ ํ•ด ์ค˜๋„ ๋˜๋Š” ๊ฑฐ๊ฒ ์ง€? ๐Ÿง

  • Math.max() : ์ตœ๋Œ€๊ฐ’
  • Math.min() : ์ตœ์†Œ๊ฐ’
  • Math.abs() : ์ ˆ๋Œ€๊ฐ’
  • Math.pow(n, m) : ๊ฑฐ๋“ญ์ œ๊ณฑ
// ๐ŸŽต 2โด ๊ตฌํ•˜๊ธฐ
Math.pow(2, 4); // 16
  • Math.sqrt(n, m) : ์ œ๊ณฑ๊ทผ
// ๐ŸŽต โˆš16 ๊ตฌํ•˜๊ธฐ
Math.sqrt(16); // 4

6. string methods

1) ๋”ฐ์˜ดํ‘œ
HTML์—” class๋ช… ๋“ฑ ํฐ๋”ฐ์˜ดํ‘œ๋กœ ๊ฐ์‹ธ์ ธ์žˆ๋Š” ๋‚ด์šฉ์ด ์žˆ์œผ๋ฏ€๋กœ ์ž‘์€๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒŒ ํŽธํ•˜๋‹ค.
์˜์–ด๋กœ ๋œ ๋ฌธ์žฅ์€ apostrophe๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ํฐ ๋”ฐ์˜ดํ‘œ๋กœ ๊ฐ์‹ธ๋Š” ๊ฒŒ ํŽธํ•˜๋‹ค.

2) backtick
backtick ์—ฌ๋Ÿฌ ์ค„์„ ์“ธ ์ˆ˜ ์žˆ์ง€๋งŒ ๋”ฐ์˜ดํ‘œ๋Š” ํ•œ ์ค„๋กœ๋งŒ ์จ์•ผ ํ•œ๋‹ค. (\n ์‚ฌ์šฉ)

3) methods

  • length
    • ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ์•Œ ์ˆ˜ ์žˆ์Œ (ํŠน์ˆ˜๋ฌธ์ž์™€ ๊ณต๋ฐฑ์„ ํฌํ•จํ•จ)
    • 0๋ถ€ํ„ฐ ์…ˆ
let hi = "์•ˆ๋…•ํ•˜์„ธ์š”.";

hi.length // 6
  • ๋ฐฐ์—ด๊ณผ ๋™์ผํ•˜๊ฒŒ ๋Œ€๊ด„ํ˜ธ ์•ˆ์— ์ˆซ์ž๋ฅผ ๋„ฃ์–ด ํŠน์ • ์œ„์น˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Œ (๊ทธ๋ ‡๋‹ค๊ณ  ๋ฌธ์ž๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋Š” ๊ฑด ์•„๋‹˜)
    • 0๋ถ€ํ„ฐ ์…ˆ
hi[2] // "ํ•˜"
  • toUpperCase() : alphabet์„ ๋ชจ๋‘ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•จ
  • toLowerCase() : alphabet์„ ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•จ
  • str.indexOf(text)
    • text๋ฅผ ๋ฌธ์ž๋กœ ๋ฐ›์•„ ๋ช‡ ๋ฒˆ์งธ์— ์œ„์น˜ํ•˜๋Š”์ง€ ๋ฐ˜ํ™˜ํ•จ (ํŠน์ˆ˜๋ฌธ์ž์™€ ๊ณต๋ฐฑ์„ ํฌํ•จํ•จ)
    • ์—†์œผ๋ฉด '-1'์„ ๋ฐ˜ํ™˜ํ•จ
    • text๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ํฌํ•จ๋˜์–ด ์žˆ์–ด๋„ ์ฒซ ๋ฒˆ์งธ ์œ„์น˜๋งŒ ๋ฐ˜ํ™˜ํ•จ
    • 0๋ถ€ํ„ฐ ์…ˆ (if๋ฌธ ์“ธ ๋•Œ ์ฃผ์˜)
let hi = "Hi, guys. Nice to meet you.";

if (hi.indexOf("Hi")) {
  console.log("์œ„ ๊ฒฐ๊ณผ๊ฐ€ 0์ด๊ณ , 0์€ if๋ฌธ์—์„œ false์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด ๋ฌธ์žฅ์ด ์ฐํžˆ์ง€ ์•Š์Œ");
}

// โ†“

if (hi.indexOf("Hi")) > -1 {
  console.log("์œ„ ๊ฒฐ๊ณผ๊ฐ€ true์—ฌ์„œ ์ด ๋ฌธ์žฅ์„ ๋ณผ ์ˆ˜ ์žˆ์Œ");
}
  • str.slice(n, m)
    • n๋ถ€ํ„ฐ m๊นŒ์ง€์˜ ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜
    • n์€ ์‹œ์ž‘์ , m์€ ๋„์ฐฉ์ ์ธ๋ฐ, m ๋ฒˆ์งธ๋ฅผ ํฌํ•จํ•˜์ง€๋Š” ์•Š์Œ (๊ทธ ์ „๊นŒ์ง€ ์…ˆ)
    • m์ด ์—†์œผ๋ฉด ๋ฌธ์ž์—ด ๋๊นŒ์ง€ ๋ฐ˜ํ™˜
    • ์Œ์ˆ˜๋ฉด ๋์—์„œ๋ถ€ํ„ฐ ์…ˆ
let desc = "abcdefg";

desc.slice(2) // "cdefg"
desc.slice(0, 5) // "abcde"
desc.slice(2, -2) // "cde"
  • str.substring(n, m)
    • str.slice(n, m)๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ n๊ณผ m์„ ๋ฐ”๊ฟ”๋„ ๋™์ž‘ํ•จ
    • ์Œ์ˆ˜๋Š” ์ธ์‹ํ•˜์ง€ ์•Š์•„์„œ 0์œผ๋กœ ๋ฐ›์•„๋“ค์ž„
  • str.substr(n, m) : n๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด m๊ฐœ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ m ๋ฒˆ์งธ๋ฅผ ํฌํ•จํ•˜์ง€๋Š” ์•Š๊ณ  ๊ทธ ์ „๊นŒ์ง€ ์…ˆ)
  • str.trim()
    • ์•ž ๋’ค ๊ณต๋ฐฑ์„ ์ œ๊ฑฐ
    • ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›์„ ๋•Œ ์ž์ฃผ ์‚ฌ์šฉ
  • str.repeat(n) : ๋ฌธ์ž์—ด์„ n๋ฒˆ ๋ฐ˜๋ณตํ•จ
  • str.slice(n, m)
  • str.includes() : ์ˆœ์„œ์— ์ƒ๊ด€ ์—†์ด ๊ด„ํ˜ธ ์•ˆ์˜ ๋ฌธ์ž๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ

4) ๋ฌธ์ž์—ด ๋น„๊ต
ASCII ํ‘œ๋Œ€๋กœ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋‹ค.
a < z, ์†Œ๋ฌธ์ž < ๋Œ€๋ฌธ์ž

"a" < "c" // true
"a".codePointAt(0); // 97
String.fromCodePoint(97) // "a"

๋ฌธ์ž์—ด์— ์“ธ ์ˆ˜ ์žˆ๋Š” methods๋ฅผ ์ด์šฉํ•ด ํ’€์–ด๋ณด๊ธฐ

// ๐ŸŽต list์—์„œ ์ˆซ์ž๋ฅผ ์ œ์™ธํ•˜๊ณ  ๋ฌธ์ž๋งŒ newList์— ๋ฐ˜ํ™˜ํ•˜๊ธฐ
let list = [
  "01. ๋“ค์–ด๊ฐ€๋ฉฐ",
  "02. JS์˜ ์—ญ์‚ฌ",
  "03. ์ž๋ฃŒํ˜•",
  "04. ํ•จ์ˆ˜",
  "05. ๋ฐฐ์—ด"
  ];

let newList = [];

for (let i = 0 ; i < list.length ; i++) {
  newList.push(list[i].slice(4));
}

console.log(newList); // ["๋“ค์–ด๊ฐ€๋ฉฐ","JS์˜ ์—ญ์‚ฌ","์ž๋ฃŒํ˜•","ํ•จ์ˆ˜","๋ฐฐ์—ด"]
// ๐ŸŽต hasCola์˜ ๋งค๊ฐœ ๋ณ€์ˆ˜์— ๊ธˆ์น™์–ด์ธ '์ฝœ๋ผ'๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ
// (1)
function hasCola(str) {
  if (str.indexOf("์ฝœ๋ผ")) {
    console.log("๊ธˆ์น™์–ด ์žˆ์Œ");
  } else {
    console.log("ํ†ต๊ณผ");
  }
}
hasCola("์‚ฌ์ด๋‹ค๊ฐ€ ์งฑ์ด์ง€"); // if๋ฌธ์—์„œ -1์€ true์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ๊ณผ๊ฐ€ "๊ธˆ์น™์–ด ์žˆ์Œ"
hasCola("์‚ฌ์ด๋‹ค ์ง€์ง€ ์ฝœ๋ผ ์งฑ"); // 
hasCola("์ฝœ๋ผ"); // if๋ฌธ์—์„œ 0์€ false์ด๊ธฐ ๋•Œ๋ฌธ์— if์— ๋ชป ๋“ค์–ด๊ฐ€๊ณ  else๋กœ ๋‚˜๊ฐ€์ ธ์„œ "ํ†ต๊ณผ"

// โ†“ ๋ฐ”๊ฟ”์ฃผ๋ฉด

// (2)
function hasCola(str) {
  if (str.indexOf("์ฝœ๋ผ") > -1) {
    console.log("๊ธˆ์น™์–ด ์žˆ์Œ");
  } else {
    console.log("ํ†ต๊ณผ");
  }
}
hasCola("์‚ฌ์ด๋‹ค๊ฐ€ ์งฑ์ด์ง€"); // "ํ†ต๊ณผ"
hasCola("์‚ฌ์ด๋‹ค ์ง€์ง€ ์ฝœ๋ผ ์งฑ"); // "ํ†ต๊ณผ"
hasCola("์ฝœ๋ผ"); // "๊ธˆ์น™์–ด ์žˆ์Œ"

// (3)
function hasCola(str) {
  if (str.includes("์ฝœ๋ผ")) {
    console.log("๊ธˆ์น™์–ด ์žˆ์Œ")
  } else {
    console.log("ํ†ต๊ณผ");
  }
}
hasCola("์‚ฌ์ด๋‹ค๊ฐ€ ์งฑ์ด์ง€"); // "ํ†ต๊ณผ"
hasCola("์‚ฌ์ด๋‹ค ์ง€์ง€ ์ฝœ๋ผ ์งฑ"); // "๊ธˆ์น™์–ด ์žˆ์Œ"
hasCola("์ฝœ๋ผ"); // "๊ธˆ์น™์–ด ์žˆ์Œ"

7. array methods 01

1) ๋ณต์Šต

  • push() ๋’ค์— ์‚ฝ์ž…, pop() ๋’ค๋ฅผ ์‚ญ์ œ, unshift() ์•ž์— ์‚ญ์ œ, shift() ์•ž์„ ์‚ญ์ œ

2) ๊ทธ ์™ธ methods

  • arr.splice(n, m) : n ~ m์˜ ์š”์†Œ๋ฅผ ์ง€์›€ (m ๋ฒˆ์งธ๋ฅผ ํฌํ•จํ•˜์ง€๋Š” ์•Š์Œ (๊ทธ ์ „๊นŒ์ง€ ์…ˆ))
  • arr.splice(n, m, x) : n ~ m์˜ ์š”์†Œ๋ฅผ ์ง€์šฐ๊ณ  x๋ฅผ ์ถ”๊ฐ€ํ•จ
// ๐ŸŽต
// (1)
let arr = [1, 2, 3, 4, 5];
arr.splice(1, 3, 100, 200); // index 1๋ถ€ํ„ฐ 3๊นŒ์ง€๋Š” ์ง€์šฐ๊ณ  ๊ทธ ์ž๋ฆฌ์— 100๊ณผ 200์„ ์‚ฝ์ž…
console.log(arr); // [1, 100, 200, 5]

// (2) ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜์— 0์„ ๋„ฃ์œผ๋ฉด ์•„๋ฌด๊ฒƒ๋„ ์‚ญ์ œ๋˜์ง€ ์•Š๊ณ  ์ถ”๊ฐ€๋˜๊ธฐ๋งŒ ํ•จ
let arr = ["๋‚˜๋Š”", "์ด๊ตญ์ข…", "์ž…๋‹ˆ๋‹ค."];

arr.splice(1, 0, "๋Œ€ํ•œ๋ฏผ๊ตญ", "์˜์‚ฌ");
console.log(arr); // ["๋‚˜๋Š”","๋Œ€ํ•œ๋ฏผ๊ตญ","์˜์‚ฌ","์ด๊ตญ์ข…","์ž…๋‹ˆ๋‹ค."]
  • arr.splice() : ์‚ญ์ œ๋œ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•จ
let arr = [1, 2, 3, 4, 5];
let result = arr.splice(1, 2);

console.log(arr); // [1, 4, 5]
console.log(result); // [2, 3]
  • arr.slice(n, m)
    • n ~ m์˜ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ (m ๋ฒˆ์งธ๋ฅผ ํฌํ•จํ•˜์ง€๋Š” ์•Š์Œ (๊ทธ ์ „๊นŒ์ง€ ์…ˆ))
    • m์„ ์“ฐ์ง€ ์•Š์œผ๋ฉด ๋ฐฐ์—ด ๋๊นŒ์ง€ ๋ฐ˜ํ™˜ํ•จ
    • ๊ด„ํ˜ธ ์•ˆ์— ์•„๋ฌด๊ฒƒ๋„ ์ ์ง€ ์•Š์œผ๋ฉด ๋ฐฐ์—ด์ด ๋ณต์‚ฌ๋จ
let arr = [1, 2, 3, 4, 5];
arr.slice(1, 4); // 2, 3, 4
  • arr.concat(arr2, arr3 ...) : ์ธ์ž๋กœ ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์ด๋‚˜ ๊ฐ’์„ ํ•ฉ์ณ์„œ ์ƒˆ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•จ
let arr = [1, 2];
arr.contact([3, 4]); // [1, 2, 3, 4]
arr.contact([3, 4], [5, 6]); // [1, 2, 3, 4, 5, 6]
arr.contact([3, 4], 5, 6); // [1, 2, 3, 4, 5, 6]
  • arr.forEach(๐‘“)
    • for๋ฌธ์ด๋‚˜ for ... of๋ฌธ ์™ธ์— forEach๋กœ๋„ ๋ฐ˜๋ณตํ•  ์ˆ˜ ์žˆ์Œ
    • ๐‘“์— ์„ธ ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ์Œ
      โ‘  ํ•ด๋‹น ์š”์†Œ
      โ‘ก ํ•ด๋‹น ์š”์†Œ์˜ index
      โ‘ข ํ•ด๋‹น ๋ฐฐ์—ด ์ž์ฒด (์‚ฌ์šฉ ๋นˆ๋„ ๋‚ฎ์Œ)
let arr = ["Ian", "Mai", "H & C"];

arr.forEach((name, index) => { // index๋Š” ์ƒ๋žตํ•ด๋„ ๋จ
  console.log(`${index + 1}. ${name}`);
});
  • arr.indexOf
    • ์ฒซ ๋ฒˆ์งธ๋กœ ๋ฐœ๊ฒฌํ•œ ๋ฌธ์ž์—ด์˜ index๋ฅผ ๋ฐ˜ํ™˜ํ•จ (์—†์œผ๋ฉด '-1'์„ ๋ฐ˜ํ™˜ํ•จ)
    • ์ธ์ˆ˜๊ฐ€ ๋‘ ๊ฐœ์ผ ๋•Œ ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋Š” ์ฐพ๊ธฐ ์‹œ์ž‘ํ•˜๋Š” ์œ„์น˜์ž„ (index๊ฐ€ ๊ฑฐ๊ธฐ์„œ๋ถ€ํ„ฐ๋ผ๋Š” ๋ง์€ ์•„๋‹˜)
let arr = [1, 2, 3, 4, 5, 1, 2, 3];

arr.indexOf(3); // 2
arr.indexOf(3, 3) // 7
arr.lastIndexOf(3) // 7
  • arr.lastIndexOf() : ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ฐœ๊ฒฌํ•œ ๋ฌธ์ž์—ด์˜ index๋ฅผ ๋ฐ˜ํ™˜ํ•จ (์—†์œผ๋ฉด '-1'์„ ๋ฐ˜ํ™˜ํ•จ)
  • arr.includes() : index ์—†์ด ํฌํ•จํ•˜๋Š”์ง€๋งŒ ํ™•์ธํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ (true or false)
  • arr.find(๐‘“)
    • ์ฐพ๋Š”๋‹ค๋Š” ์˜๋ฏธ๋Š” ๋™์ผํ•˜์ง€๋งŒ ์กฐ๊ธˆ ๋” ๋ณต์žกํ•œ ์—ฐ์‚ฐ์„ ํ•  ์ˆ˜ ์žˆ์Œ
    • ์ฒซ ๋ฒˆ์งธ true๋งŒ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๋๋‚˜๋ฏ€๋กœ ์ฃผ์˜ํ•ด์•ผ ํ•จ
    • ์—†์œผ๋ฉด undefined๋ฅผ ๋ฐ˜ํ™˜ํ•จ
let arr = [1, 2, 3, 4, 5, 6];
const result = arr.find((item) => {
  return item % 2 === 0;
});

console.log(result); // 2 (4์™€ 6์€ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์Œ)
// ๐ŸŽต ๋ฏธ์„ฑ๋…„์ž์ธ user ๋ฐ˜ํ™˜ํ•˜๊ธฐ
let userList = [
  { name : "Mai", age: 37 },
  { name : "Ian", age: 38 },
  { name : "H & C", age: 7 }
];

const result = userList.find((user) => {
  if (user.age < 19) {
    return true;
  }
  return false;
});
console.log(result); // {"name": "H & C", "age": 7}
  • arr.findIndex(๐‘“)
    • ์ฐพ๋Š”๋‹ค๋Š” ์˜๋ฏธ๋Š” ๋™์ผํ•˜์ง€๋งŒ ์กฐ๊ธˆ ๋” ๋ณต์žกํ•œ ์—ฐ์‚ฐ์„ ํ•  ์ˆ˜ ์žˆ์Œ
    • ์žˆ์œผ๋ฉด index๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์—†์œผ๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•จ
    • ์ฒซ ๋ฒˆ์งธ true๋งŒ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๋๋‚˜๋ฏ€๋กœ ์ฃผ์˜ํ•ด์•ผ ํ•จ
// ๐ŸŽต ๋ฏธ์„ฑ๋…„์ž์ธ user์˜ index ๋ฐ˜ํ™˜ํ•˜๊ธฐ
let userList = [
  { name : "Mai", age: 37 },
  { name : "Ian", age: 38 },
  { name : "H & C", age: 7 }
];

const result = userList.findIndex((user) => {
  if (user.age < 19) {
    return true;
  }
  return false;
});
console.log(result); // 2
  • arr.filter(๐‘“)
    • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•จ
    • ์‚ฌ์šฉ๋ฒ•์€ arr.find(๐‘“)์™€ ๋™์ผํ•จ
const arr = [1, 2, 3, 4, 5, 6];

const result = arr.filter((item) => {
  return item % 2 === 0;
});

console.log(result); // [2, 4, 6]

๐Ÿง find๋Š” ์ˆซ์ž๋กœ ๋ฐ˜ํ™˜๋˜๋”๋‹ˆ filter๋Š” ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜๋˜๋„ค...? ๊ฐ™์€ array methods์ธ๋ฐ... 2์™€ [2]๋Š” ๊ฐ™์ง€ ์•Š์€๋ฐ... ํ—ท๊ฐˆ๋ฆฌ๊ฒŒ! ์ด๋ ‡๊ฒŒ ์ •ํ•œ ์ด์œ ๊ฐ€ ์žˆ์„ ํ…๋ฐ ๊ทธ๊ฒŒ ๋ญ”์ง€ ๋ชจ๋ฅด๊ฒ ๋‹คใ… ใ…  ๐Ÿง

  • arr.reverse() : ์—ญ์ˆœ์œผ๋กœ ์žฌ์ •๋ ฌํ•จ (์ตœ๊ทผ์— ๊ฐ€์ž…ํ•œ user ์ˆœ์œผ๋กœ ์ •๋ ฌํ•  ๋•Œ, ์ตœ๊ทผ์— ์ž‘์„ฑ๋œ ๊ธ€ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•  ๋•Œ ์ž์ฃผ ์“ฐ์ž„)
  • arr.map(๐‘“) : ํ•จ์ˆ˜๋ฅผ ๋ฐ›์•„ ํŠน์ • ๊ธฐ๋Šฅ์„ ์‹œํ–‰ํ•˜๊ณ  ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•จ
let userList = [
  { name : "Mai", age: 37 },
  { name : "Ian", age: 38 },
  { name : "H & C", age: 7 }
];

let newUserList = userList.map((user, index) => {
  return Object.assign({}, user, {
    id : index + 1,
    isAdult : user.age > 19,
  });
});

console.log(newUserList);
  • arr.join()
    • ๋ฐฐ์—ด์„ ํ•ฉ์ณ์„œ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜๋“ค ๋•Œ ์‚ฌ์šฉํ•จ
    • ์ธ์ˆ˜๋กœ ๊ตฌ๋ถ„์ž๋ฅผ ๋ฐ›์Œ
let arr = ["์•ˆ๋…•", "๋‚˜๋Š”", "๋ฉ”์ดํ˜ธ์•ผ"];
let result = arr.join("โค๏ธŽ");
console.log(result); // "์•ˆ๋…•โค๏ธŽ๋‚˜๋Š”โค๏ธŽ๋ฉ”์ดํ˜ธ์•ผ"
  • arr.split() : ๋ฌธ์ž์—ด์„ ๋‚˜๋ˆ ์„œ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์คŒ
// (1)
const users = "Mai, Ian, H & C";
const result = users.split(",");
console.log(result); // ["Mai"," Ian"," H & C"]

// (2)
let str = "Hello, this is KARI.";
const result = str.split("");
console.log(result); // ["H","e","l","l","o",","," ","t","h","i","s"," ","i","s"," ","K","A","R","I","."]
  • Array.isArray()
    • ๋ฐฐ์—ด์ธ์ง€ ์•„๋‹Œ์ง€ ํŒ๋‹จํ•  ๋•Œ ์‚ฌ์šฉํ•จ
    • JavaScript๋Š” ๋ฐฐ์—ด์„ ๊ฐ์ฒด๋ผ๊ณ  ํŒ๋‹จํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด์— typeof๋ฅผ ์ ์šฉํ•˜๋ฉด ๋ชจ๋‘
      ๊ฐ์ฒด๋ผ๊ณ  ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ ๋ฐฐ์—ด์ธ์ง€ ์•„๋‹Œ์ง€ ์•Œ๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ๋จ
let me = {
  name : "Mai",
  age : 31
};

let us = ["Mai", "Ian", "H & C"];

console.log(typeof me); // "object"
console.log(typeof us); // "object"

console.log(Array.isArray(me)); // false
console.log(Array.isArray(us)); // true

8. array methods 02 - sort, reduce

  • arr.sort(๐‘“)
    • ๋ฐฐ์—ด์„ ์žฌ์ •๋ ฌํ•จ (๋ฐฐ์—ด ์ž์ฒด๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฏ€๋กœ ์ฃผ์˜)
    • ํ•จ์ˆ˜๋ฅผ ์ธ์ˆ˜๋กœ ๋ฐ›์Œ
// ๐ŸŽต '27, 8, 5, 13'์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ
let arr = [27, 8 , 5, 13];

arr.sort();
console.log(arr); // [13, 27, 5, 8] : ์•ž์ž๋ฆฌ๊ฐ€ 1, 2์—ฌ์„œ 13๊ณผ 27์ด ์•ž์— ์˜ด

// โ†“

// (1) ํ•จ์ˆ˜๋ฅผ arr ์™ธ๋ถ€์— ์ž‘์„ฑ
let arr [27, 8, 5, 13];

function fn(a, b) {
  return a-b; // 'a-b'๊ฐ€ ์–‘์ˆ˜๋ฉด a๊ฐ€ b ๋’ค์—, 0์ด๋ฉด ๊ทธ๋Œ€๋กœ, ์Œ์ˆ˜๋ฉด a๊ฐ€ b ์•ž์— ์˜ค๋„๋ก ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•˜ ํ•จ์ˆ˜
}

arr.sort(fn);
console.log(arr); // [5, 8, 13, 27]

// (2) ํ•จ์ˆ˜๋ฅผ arr ๋‚ด๋ถ€์— ์ž‘์„ฑ
let arr [27, 8, 5, 13];

arr.sort((a-b) => {
  // ์—ฌ๊ธฐ์— 'console.log'๋ฅผ ์ฐ์–ด๋ณด๋ฉด ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์ด ๋‘ ์ˆ˜์”ฉ ๋ฌถ์—ฌ ์žˆ์Œ (์•ž ์ˆซ์ž๊ฐ€ a, ๋’ค ์ˆซ์ž๊ฐ€ b)
  return a-b;
});
console.log // [5, 8, 13, 27]

// (3) Lodash ํ™œ์šฉ
// '<script src="lodash.js"></script>' ์ž‘์„ฑํ•˜๊ณ  ์จ์•ผ ํ•จ

let arr = [27, 8 , 5, 13];

_.sortBy(arr);
console.log(arr);// [5, 8, 13, 27]
  • arr.reduce(๐‘“)
    • (๋ˆ„์  ๊ณ„์‚ฐ ๊ฐ’, ํ˜„์žฌ ๊ฐ’) => { return ๊ณ„์‚ฐ ๊ฐ’ };
    • ํ•จ์ˆ˜๋ฅผ ์ธ์ˆ˜๋กœ ๋ฐ›์Œ
    • arr.map(๐‘“)์ด๋‚˜ arr.filter(๐‘“) ๋Œ€์‹  ์จ์„œ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์Œ
// ๐ŸŽต ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์ˆ˜ ํ•ฉ์น˜๊ธฐ
let arr = [1, 2, 3, 4, 5];

const result = arr.reduce((prev, crr) => {
  return prev + crr;
}, 0); // ์—ฌ๊ธฐ์—์„œ '0'์€ ์ดˆ๊ธฐ๊ฐ’์ธ๋ฐ, ์›ํ•˜๋Š” ๊ฒƒ์„๋กœ ๋ฐ”๊พธ์–ด๋„ ๋จ e.g. 100, [] ๋“ฑ
console.log(result); // 15

// ๐ŸŽต ์„ฑ์ธ์˜ ์ด๋ฆ„๋งŒ ๋„์ถœํ•˜๊ธฐ
let userList = [
  { name : "Ian", age: 45 },
  { name : "Mai", age: 44 },
  { name : "H", age: 18 },
  { name : "C", age: 16 }
];

let result = userList.reduce((prev, cur) => {
  if (cur.age > 18) {
    prev.push(cur.name);
  }
  return prev;
}, []);
console.log(result); // ["Ian","Mai"]

// ๐ŸŽต ์‚ฌ์šฉ์ž์˜ ๋‚˜์ด์˜ ํ•ฉ ๊ตฌํ•˜๊ธฐ
let userList = [
  { name : "Ian", age: 45 },
  { name : "Mai", age: 44 },
  { name : "H", age: 18 },
  { name : "C", age: 16 }
];

let result = userList.reduce((prev, cur) => {
  return (prev += cur.age);
}, 0);
console.log(result); // 123

// ๐ŸŽต ์ด๋ฆ„์˜ ๊ธธ์ด๊ฐ€ ์„ธ ๊ธ€์ž์ธ ์‚ฌ๋žŒ ๊ตฌํ•˜๊ธฐ
let userList = [
  { name : "Ian", age: 45 },
  { name : "Mai", age: 44 },
  { name : "H", age: 18 },
  { name : "C", age: 16 }
];

let result = userList.reduce((prev, cur) => {
  if (cur.name.length === 3) {
    prev.push(cur.name);
  } return prev;
}, []);
console.log(result); // ["Ian","Mai"]

cf. arr.reduceRight() ๋„ arr.reduce์™€ ๋น„์Šทํ•˜๋‚˜ ๋ฐฐ์—ด์˜ ๋์—์„œ๋ถ€ํ„ฐ ์‹œ์ž‘๋จ


9. destructuring assignment

  • ์ •์˜
    • ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น ๊ตฌ๋ฌธ์€ ๋ฐฐ์—ด์ด๋‚˜ ๊ฐ์ฒด์˜ ์†์„ฑ์„ ๋ถ„ํ•ดํ•ด์„œ ๊ทธ ๊ฐ’์„ ๋ณ€์ˆ˜์— ๋‹ด์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ํ‘œํ˜„์‹
  • ๋ฐฐ์—ด ๊ตฌ์กฐ ๋ถ„ํ•ด
let str = "Ian - Mai - H - C";
let [user1, user2, user3, user4] = str.split(" - ");
console.log(user1); // "Ian"
console.log(user2); // "Mai"
console.log(user3); // "H"
console.log(user4); // "C"
  • ๊ธฐ๋ณธ ๊ฐ’
    • ๊ธฐ๋ณธ ๊ฐ’์ด ์—†์œผ๋ฉด ๊ฐ’์ด ์—†์„ ๋•Œ 'undefined'๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ ๊ธฐ๋ณธ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋ฉด ์˜ค๋ฅ˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Œ
let [a, b, c] = [1, 2];
console.log(a); // 1
console.log(b); // 2
console.log(c); // undefined

// โ†“

let [a=3, b=4, c=5] = [1, 2];
console.log(a); // 1
console.log(b); // 2
console.log(c); // 5
  • ๊ณต๋ฐฑ๊ณผ comma๋ฅผ ์ด์šฉํ•œ ๋ฐ˜ํ™˜ ๊ฐ’ ๋ฌด์‹œ
let [user1, , user2, user3, user4] = ["Ian", "Krystal", "Mai", "H", "C"];
console.log(user1); // "Ian"
console.log(user2); // "Mai"
console.log(user3); // "H"
console.log(user4); // "C"
  • ๊ฐ์ฒด ๊ตฌ์กฐ ๋ถ„ํ•ด
let user = {name: "Mai", age: 30};
let {name, age} = user; // 'let {age, name} = user;'๋ผ๊ณ  ์ˆœ์„œ๋ฅผ ๋ฐ”๊ฟ”๋„ 'let name = user.name, let age = user.age'์ธ ๊ฒƒ์— ๋ณ€ํ•จ ์—†์Œ
console.log(name); // "Mai"
console.log(age); // 30
  • key ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ
let user = {name: "Mai", age: 30};
let {name:userName, age:userAge} = user; 
console.log(userName); // "Mai"
console.log(userAge); // 30
  • ๊ธฐ๋ณธ ๊ฐ’
    • ๊ธฐ๋ณธ ๊ฐ’์ด ์—†์œผ๋ฉด ๊ฐ’์ด ์—†์„ ๋•Œ 'undefined'๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ ๊ธฐ๋ณธ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋ฉด ์˜ค๋ฅ˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Œ
    • ๊ฒฐ๊ณผ๊ฐ€ 'undefined'์ผ ๋•Œ๋งŒ ๊ธฐ๋ณธ ๊ฐ’์ด ์‚ฌ์šฉ๋˜๋ฏ€๋กœ ์ด๋ฏธ ๊ฐ์ฒด์— ๊ฐ’์ด ์žˆ๋‹ค๋ฉด ๊ธฐ๋ณธ ๊ฐ’์€ ๋ฌด์‹œ๋จ
let user = {name: "Mai", age: 30};
let {name, age, gender} = user;
console.log(name); // "Mai"
console.log(age); // 30
console.log(gender); // undefined

// โ†“

let user = {name: "Mai", age: 30};
let {name, age, gender = "gender"} = user;
console.log(name); // "Mai"
console.log(age); // 30
console.log(gender); // "gender"

10. rest parameters, spread syntax

  • arguments๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋˜๋‚˜ ES6์—์„  ๋‚˜๋จธ์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์„ ๊ถŒ์žฅํ•จ
    • ๋‚˜๋จธ์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์ •ํ•ด์ง€์ง€ ์•Š์€ ๊ฐœ์ˆ˜์˜ ์ธ์ˆ˜๋ฅผ ๋ฐฐ์—ด๋กœ ๋‚˜ํƒ€๋ƒ„
    • arguments์™€ ๋‹ฌ๋ฆฌ ๋ฐฐ์—ด methods๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ
    • ๋‚˜๋จธ์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ•ญ์ƒ ๋งˆ์ง€๋ง‰์— ์žˆ์–ด์•ผ ํ•จ
function showName(...names){
  console.log(names);
}

showName(); // []
showName("Ian"); // ["Ian"]
showName("Ian, Mai"); // ["Ian", "Mai"]

// ๐ŸŽต ์ „๋‹ฌ ๋ฐ›์€ ์ˆ˜ ๋ชจ๋‘ ๋”ํ•˜๊ธฐ (1)
function add(...numbers) {
  let result = 0; // ์ดˆ๊ธฐ ๊ฐ’์€ 0
  numbers.forEach((num) => (result += num));
  console.log(result);
}

add(1, 2, 3); // 6
add(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // 55

// ๐ŸŽต ์ „๋‹ฌ ๋ฐ›์€ ์ˆ˜ ๋ชจ๋‘ ๋”ํ•˜๊ธฐ (2)
function add(...numbers) {
  let result = numbers.reduce((prev, cur) => prev + cur);
  console.log(result);
}

add(1, 2, 3); // 6
add(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // 55

// ๐ŸŽต user ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋งŒ๋“ค๊ธฐ
function User(name, age, ...skills) {
  this.name = name;
  this.age = age;
  this.skills = skills;
}

const user1 = new User("Ian", 30, "TS", "React");
const user2 = new User("Mai", 29, "Three", "React");
const user3 = new User("H", 16, "English");
const user4 = new User("C", 14, "French");
console.log(user1);
console.log(user2);
console.log(user3);
console.log(user4);
  • ์ „๊ฐœ ๊ตฌ๋ฌธ
    • arr.push(), arr.splice(), arr.concat9) ์ด๋Ÿฐ method๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฒˆ๊ฑฐ๋กœ์šด๋ฐ ์ „๊ฐœ ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด ์‰ฌ์›€
    • ๊ฐ์ฒด์—์„œ๋„ Object.assign ์“ธ ํ•„์š” ์—†์Œ
let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];

let result = [...arr1, ...arr2]; // '...arr1' = 1, 2, 3์ด๊ณ  '...arr2' = 4, 5, 6
console.log(result); // [1, 2, 3, 4, 5, ,6]

// ๐ŸŽต arr1์„ [4, 5, 6, 1, 2, 3]์œผ๋กœ ๋งŒ๋“ค๊ธฐ
// (1)
let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];

arr2.reverse().forEach((num) => {
  arr1.unshift(num);
});
console.log(arr1);

// (2)
let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];

arr1 = [...arr2, ...arr1];
console.log(arr1);

// ๐ŸŽต fe์™€ lang์„ skills๋กœ ํ•˜์—ฌ ๊ฐ์ฒด ๋งŒ๋“ค๊ธฐ
// (1)
let user = { name: "Mai"};
let info = { age: 30 };
let fe = ["Three", "React"];
let lang = ["Korean", "English"];

user = Object.assign({}, user, info, {
  skills: [],
});

fe.forEach((item) => {
  user.skills.push(item);
});
lang.forEach((item) => {
  user.skills.push(item);
});
console.log(user);

// (2)
let user = { name: "Mai"};
let info = { age: 30 };
let fe = ["Three", "React"];
let lang = ["Korean", "English"];

user = {
  ...user,
  ...info,
  skills : [
  ...fe, ...lang],
};
console.log(user);

11. closure

โ€ป ๋ณ€์ˆ˜, hoisting, ํ•จ์ˆ˜ ์„ ์–ธ๋ฌธ์˜ ํŠน์ง•์— ๋Œ€ํ•œ ์„ ํ–‰ ํ•™์Šต ํ•„์š”

  • ์ •์˜
    • ํ•จ์ˆ˜์™€ ์–ดํœ˜์  ํ™˜๊ฒฝ (lexical environment) ์˜ ์กฐํ•ฉ (JS๋Š” ์–ดํœ˜์  ํ™˜๊ฒฝ์ž„)
    • ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑ๋  ๋‹น์‹œ์˜ ์™ธ๋ถ€ ๋ณ€์ˆ˜๋ฅผ ๊ธฐ์–ตํ•˜๋ฉฐ, ์ƒ์„ฑ๋œ ์ดํ›„์—๋„ ๊ณ„์†ํ•ด์„œ ๊ทธ ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Œ (์™ธ๋ถ€ ํ•จ์ˆ˜์˜ ์‹คํ–‰์ด ๋๋‚˜์„œ ์™ธ๋ถ€ ํ•จ์ˆ˜๊ฐ€ ์†Œ๋ฉธ๋œ ์ดํ›„์—๋„ ๋‚ด๋ถ€ ํ•จ์ˆ˜๊ฐ€ ์™ธ๋ถ€ ํ•จ์ˆ˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Œ)

๐Ÿง ์˜ค... ์–ด๋ ต๋‹ค.. ๋‹ค์‹œ ๋ด์•ผ๊ฒ ๋‹ค... ๐Ÿง

function makeCounter() {
  let num = 0; // 2๏ธโƒฃ ๊ทธ ์ˆซ์ž๋Š” ์™ธ๋ถ€ ํ•จ์ˆ˜์˜ ๋ณ€์ˆ˜์ž„, 5๏ธโƒฃ '์€๋‹‰ํ™”'์— ์„ฑ๊ณตํ•œ ๊ฒƒ (์˜ค์ง counter๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๊ณ  ๋ฐ˜ํ™˜๋ฐ›์„ ์ˆ˜๋งŒ ์žˆ์Œ)
  
  return function () {
    return num++;
  };
}

let counter = makeCounter(); // 1๏ธโƒฃ counter์— makeCounter๊ฐ€ returnํ•˜๋Š” ํ•จ์ˆ˜์ธ function () { return num++; };์„ ๋„ฃ์Œ, ์ด ํ•จ์ˆ˜๋Š” ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ 4๏ธโƒฃ ์ด๋ ‡๊ฒŒ counter๊ฐ€ ์ƒ์„ฑ๋œ ์ดํ›„์— ๊ณ„์† ๊ธฐ์–ตํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— 0, 1, 2, ์ด๋ ‡๊ฒŒ ์ฆ๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ž„
console.log(counter()); // 0 3๏ธโƒฃ ์ดˆ๊ธฐ ๊ฐ’์ธ 0 ์ดํ›„์— 1์”ฉ ๋”ํ•ด์ง€๋Š” ๊ฑด ๋‚ด๋ถ€ ํ•จ์ˆ˜์—์„œ (return num++) ์™ธ๋ถ€ ํ•จ์ˆ˜์ธ makeCounter() { let num = 0; ์ฆ‰ ์™ธ๋ถ€ ํ•จ์ˆ˜์˜ num์— ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ
console.log(counter()); // 1
console.log(counter()); // 2

12. scheduling functions - setTimeout, setInterval

  • ๊ณตํ†ต ํŠน์ง•
    • ํ˜„์žฌ ์‹คํ–‰ ์ค‘์ธ script๊ฐ€ ์ข…๋ฃŒ๋œ ํ›„ scheduling ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•จ
    • brower๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ 4ms ์ •๋„์˜ ๋Œ€๊ธฐ ์‹œ๊ฐ„์ด ์žˆ์–ด์„œ time์ด 0์ด์–ด๋„ ๋ฐ”๋กœ ์‹คํ–‰๋˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Œ
  • setTimeout(๐‘“, time);
    • ์ผ์ • ์‹œ๊ฐ„์ด ์ง€๋‚œ ํ›„ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰
    • ์ธ์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋ฉด 'time' ๋’ค์— ๊ธฐ์žฌ
// ๐ŸŽต ํ•จ์ˆ˜๋ฅผ 3์ดˆ ํ›„์— ์‹คํ–‰ํ•˜๊ธฐ
function fn() {
  console.log(3)
}

setTimeout(fn, 3000); // 3000๋Š” 3์ดˆ์ž„

// โ†“ ์ด๋ ‡๊ฒŒ ์ ์–ด๋„ ๋จ
setTimeout(function() {
  console.log(3)
}, 3000);

// ๐ŸŽต ์ธ์ˆ˜ ์ถ”๊ฐ€ํ•˜๊ธฐ
function showName(name) {
  console.log(name);
}

setTimeout(showName, 3000, "Mai"); // ์ˆœ์„œ๋Œ€๋กœ ํ•จ์ˆ˜, ์‹œ๊ฐ„, ์ธ์ˆ˜์ด๋ฉฐ ์ด ์ธ์ˆ˜๋Š” showName ํ•จ์ˆ˜์˜ ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜ (name) ๋กœ ์ „๋‹ฌ๋จ
  • clearTimeout(tId) : ์˜ˆ์ •๋œ ์ž‘์—…์„ ์—†์•ฐ
// ๐ŸŽต showName ํ•จ์ˆ˜ ์‹คํ–‰ํ•˜์ง€ ์•Š๊ธฐ
const tId = function showName(name) {
  console.log(name);
  clearTimeout(tId);
}

setTimeout(showName, 3000, "Mai");

๐Ÿง mozilla๋ฅผ ๋ณด๋‹ˆ cancel property๊ฐ™์€ ๊ณณ์— ์‚ฌ์šฉํ•˜๋Š” ๋“ฏํ•จ ๐Ÿง

  • setInterval
    • ์ผ์ • ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ๋ฐ˜๋ณต
    • setTimeout๊ณผ ์‚ฌ์šฉ ๋ฐฉ๋ฒ• ๋™์ผ
    • ๋ฐ˜๋ณต์„ ์ค‘๋‹จํ•˜๋ ค๋ฉด clearInterval(tId)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋จ
// ๐ŸŽต 3์ดˆ๋งˆ๋‹ค Mai ๋‚˜์˜ค๊ฒŒ ํ•˜๊ธฐ
function showName(name) {
  console.log(name);
}

const tId = setInterval(showName, 3000, "Mai");

// ๐ŸŽต ์‚ฌ์šฉ์ž๊ฐ€ ์ ‘์†ํ•œ ์ง€ ์–ผ๋งˆ๋‚˜ ์ง€๋‚ฌ๋Š”์ง€ 1์ดˆ์— ํ•œ ๋ฒˆ์”ฉ 5์ดˆ ๋™์•ˆ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
let num = 0;

function showTime() {
  console.log(`์ ‘์†ํ•œ ์ง€ ${num++}์ดˆ๊ฐ€ ์ง€๋‚ฌ์Šต๋‹ˆ๋‹ค.`);
  if (num > 5) {
    clearInterval(tId);
  }
}

const tId = setInterval(showTime, 1000);

13. this ๊ฐ’์„ ๋ฐ”๊พธ๋Š” ํ•จ์ˆ˜๋“ค - call, apply, bind

JS์—์„œ๋Š” ํ•จ์ˆ˜๋ฅผ ์–ด๋””์—์„œ ์–ด๋–ป๊ฒŒ ํ˜ธ์ถœํ–ˆ๋Š”์ง€์™€ ์ƒ๊ด€ ์—†์ด this๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • call
    • ๋ชจ๋“  ํ•จ์ˆ˜์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, this๋ฅผ ํŠน์ • ๊ฐ’์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Œ
    • ์ฐจ๋ก€๋Œ€๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋“ค์–ด๊ฐ€์•ผ ํ•จ
// ๐ŸŽต ์ด๋ฆ„ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
const mai = {
  name : "Mai",
};

const ian = {
  name : "Ian",
};

function showThisName() {
  console.log(this.name);
}

showThisName(); // (๋นˆ ๊ฐ’) : ์—ฌ๊ธฐ์—์„œ this๋Š” window๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š”๋ฐ, window.name์€ ๋นˆ ๊ฐ’์ด๋ผ์„œ ์•„๋ฌด๊ฒƒ๋„ ๋œจ์ง€ ์•Š์Œ
showThisName.call(mai) // Mai : ์ด๋•Œ, ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ call์„ ์‚ฌ์šฉํ•˜๊ณ  this๋กœ ์‚ฌ์šฉํ•  ๊ฐ์ฒด์ธ mai๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„ ๊ฐ์ฒด์˜ method์ธ ๊ฒƒ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ. call์˜ ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” this๋กœ ์‚ฌ์šฉํ•  ๊ฐ’์ด๊ณ , ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋” ์žˆ์œผ๋ฉด ๊ทธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ˜ธ์ถœํ•˜๋Š” ํ•จ์ˆ˜๋กœ ์ „๋‹ฌ๋จ (this๊ฐ€ mai๊ฐ€ ๋œ ๊ฒƒ)
showThisName.call(ian) // Ian : this๊ฐ€ ian์ด ๋จ

// ๐ŸŽต ์ด๋ฆ„์— ์ฃผ์†Œ์™€ ์ง์—… ์ถ”๊ฐ€ํ•˜๊ธฐ
const mai = {
  name : "Mai",
};

const ian = {
  name : "Ian",
};

function update(address, occupation) { // update๋Š” address์™€ occupation์„ ๋ฐ›์•„์„œ ๊ฐ์ฒด ์ •๋ณด๋ฅผ ์ƒˆ๋กœ์šด date๋กœ updateํ•ด์คŒ
  this.address = address;
  this.occupation = occupation;
};

update.call(mai, "Swiss", "professor"); // ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” this๋กœ ์‚ฌ์šฉ๋  ๊ฐ’, ๋‘ ๋ฒˆ์งธ์™€ ์„ธ ๋ฒˆ์งธ๋Š” ํ•จ์ˆ˜๊ฐ€ ์‚ฌ์šฉํ•  ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๊ธฐ์žฌํ•œ ๊ฒƒ
console.log(mai);

update.call(ian, "Italy", "photographer");
console.log(ian);
  • apply
    • ํ•จ์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ์™ธํ•˜๋ฉด call๊ณผ ๋™์ผํ•จ
    • call์€ ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์ง€๋งŒ, apply๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ›์Œ
    • apply๋Š” ๋ฐฐ์—ด ์š”์†Œ๋ฅผ ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•  ๋•Œ ์šฉ์ดํ•จ
    • ๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐฐ์—ด์„ ์ „๋‹ฌํ•˜๋ฉด ์ฐจ๋ก€๋Œ€๋กœ ๊ทธ ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์„ ์ธ์ˆ˜๋กœ ์‚ฌ์šฉํ•จ
// ๐ŸŽต ์œ„์™€ ๋™์ผํ•œ ๊ฐ’์„ ์–ป๋˜ call์ด ์•„๋‹Œ apply ์‚ฌ์šฉํ•˜๊ธฐ
const mai = {
  name : "Mai",
};

const ian = {
  name : "Ian",
};

function update(address, occupation) {
  this.address = address;
  this.occupation = occupation;
};

update.call(mai, ["Swiss", "professor"]); // ๋ฐฐ์—ด๋กœ ์ ์–ด์•ผ ํ•จ
console.log(mai);

update.call(ian, ["Italy", "photographer"]); // ๋ฐฐ์—ด๋กœ ์ ์–ด์•ผ ํ•จ
console.log(ian);

// ๐ŸŽต ๋‹ค์Œ ์ˆ˜์˜ ์ตœ์†Œ๊ฐ’๊ณผ ์ตœ๋Œ€๊ฐ’ ๊ตฌํ•˜๊ธฐ
const nums = [3, 10, 1, 6, 4];

// (1)
const minNum = Math.min(...nums);
const maxNum = Math.max(...nums);

// (2)
const minNum = Math.min.call(null, ...nums);
const maxNum = Math.max.call(null, ...nums);
//  = Math.max.apply(null, [3, 10, 1, 6, 4])

// (3)
const minNum = Math.min.apply(null, nums);
const maxNum = Math.max.apply(null, nums);
//  = Math.max.call(null, 3, 10, 1, 6, 4)

console.log(minNum);
console.log(maxNum);
  • bind : ํ•จ์ˆ˜์˜ this ๊ฐ’์„ ์˜๊ตฌํžˆ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Œ
// ๐ŸŽต "hello, Mai" ๋„์ถœํ•˜๊ธฐ
const user = {
  name: "Mai",
  showName: function () {
    console.log(`hello, ${this.name}`);
  },
};

// (1)
user.showName(); // "hello, Mai"

let fn = user.showName;
fn(); // ๋ฐ˜๋ฉด ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด "hello, "๋งŒ ๋‚˜์˜ค๋Š”๋ฐ ๊ทธ ์ด์œ ๋Š” method๋Š” ์  ์•ž์— ์žˆ๋Š” ๊ฒŒ this์ธ๋ฐ ์œ„์—์„œ (let fn = ) fn์„ ํ• ๋‹นํ•  ๋•Œ this๋ฅผ ์žƒ์–ด๋ฒ„๋ ธ๊ณ  ํ˜ธ์ถœํ•  ๋•Œ fn()๋งŒ ํ˜ธ์ถœํ•˜๋‹ˆ๊นŒ this๊ฐ€ ์—†๋Š” ๊ฒƒ

//(2)
fn.call(user); // "hello, Mai"

// (3)
fn.apply(user); // "hello, Mai"

// (4)
let boundFn = fn.bind(user);

boundFn(); // "hello, Mai"

๐Ÿง ์™œ ๊ทธ๋ƒฅ fn.bind(user);๋Š” ์•ˆ ๋˜์ง€? ๐Ÿง


14. ์ƒ์†, prototype


์ถœ์ฒ˜ : ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ค‘๊ธ‰ ๊ฐ•์ขŒ #14 ์ƒ์†, ํ”„๋กœํ† ํƒ€์ž…(Prototype)

// ๐ŸŽต for ... in์œผ๋กœ ๊ฐ์ฒด์˜ property ์ˆœํšŒํ•˜๊ธฐ
const car = {
  wheels: 4,
  drive() {
    console.log("drive..");
  },
};

const bmw = {
  color: "red",
  navigation: 1,
};

bmw.__proto__ = car; // bmw ๋ณ€์ˆ˜์—๋Š” ์—†์ง€๋งŒ car์— ์žˆ๋Š” wheels์™€ drive ์ •๋ณด๋„ bmw๊ฐ€ ๊ฐ–๊ฒŒ ๋จ

const x5 = {
  color: "white",
  name: "x5",
};

x5.__proto__ = bmw;

for(p in x5) {
  console.log(p);
}
// โฌ‡๏ธ Output
// "color"
// "name"
// "navigation"
// "wheels"
// "drive"

// ํ•˜์ง€๋งŒ key, value ๋“ฑ ๊ฐ์ฒด ๊ด€๋ จ method๋Š” ์ƒ์†๋œ property๋Š” ์•ˆ ๋‚˜์˜ด
Object.keys(x5); // ["color", "name"]
Object.values(x5); // ["white", "x5"]

// ๐ŸŽต for ... in์œผ๋กœ ๊ฐ์ฒด์˜ property ์œ ๋ฌด ํ™•์ธํ•˜๊ธฐ
for (p in x5) {
  if (x5.hasOwnProperty(p)) {
    console.log("o", p);
  } else {
    console.log("x", p);
  }
}
// โฌ‡๏ธ Output
// "o" "color"
// "o" "name"
// "x" "navigation"
// "x" "wheels"
// "x" "drive"

// ๐ŸŽต ๋น„์Šทํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜์™€ ์‚ฌ์šฉํ•˜๊ธฐ
const car = {
  wheels: 4.
  drive() {
    console.log("drive..");
  },
};

const Bmw = function (color) {
  this.color = color;
};

const x5 = new Bmw("red");
const z4 = new Bmw("blue");

x5.__proto__ = car;
z4.__proto__ = car;

// โ†“ ๊ฐ™์€ ๋™์ž‘์ธ๋ฐ ์ผ์ผ์ด '__proto__' ์•ˆ ์จ๋„ ๋˜๊ฒŒ code ๋ณ€๊ฒฝ

const Bmw = function (color) {
  this.color = color;
};

Bmw.prototype.wheels = 4;
Bmw.prototype.drive = function () {
  console.log("drive..");
};

const x5 = new Bmw("red");
const z4 = new Bmw("blue");
  • instanceof
    • ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๋•Œ ๊ทธ ๊ฐ์ฒด๋Š” ์ƒ์„ฑ์ž์˜ instance๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š”๋ฐ, ์ด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ์—ฐ์‚ฐ์ž์ž„
    • ๊ฐ์ฒด์™€ ์ƒ์„ฑ์ž๋ฅผ ๋น„๊ตํ•˜๊ณ , ๊ฐ์ฒด๊ฐ€ ๊ทธ ์ƒ์„ฑ์ž๋กœ๋ถ€ํ„ฐ ์ƒ์„ฑ๋œ ๊ฒƒ์ธ์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ
const Bmw = function (color) {
  this.color = color;
};

Bmw.prototype.wheels = 4;
Bmw.prototype.drive = function () {
  console.log("drive..");
};

const x5 = new Bmw("red");
const z4 = new Bmw("blue");

z4 instanceof Bmw // true
z4.constructor === Bmw // true : ์ƒ์„ฑ์ž๋กœ ๋งŒ๋“ค์–ด์ง„ instance ๊ฐ์ฒด์—๋Š” constructor๋ผ๋Š” property๊ฐ€ ์กด์žฌํ•จ

// โ†“ ๊ฐ™์€ ๋™์ž‘์ธ๋ฐ ํ•œ ๋ฒˆ์— ์“ฐ๋„๋ก code๋ฅผ ๋ณ€๊ฒฝ...ํ•  ์ˆ˜ ์žˆ์œผ๋‚˜ ์ด๋ ‡๊ฒŒ ๋ฎ์–ด์“ฐ๊ธฐ ํ•˜๋ฉด constructor๊ฐ€ ์‚ฌ๋ผ์ง. ๊ทธ๋ž˜์„œ ์œ„์™€ ๊ฐ™์ด ์“ฐ๋“ ์ง€, ์•„๋‹ˆ๋ฉด ๋”ฐ๋กœ constructor๋ฅผ ์ ์–ด์ฃผ์–ด์•ผ ํ•จ. ์ฆ‰ JS๋Š” constructor๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ๋ณด์žฅํ•˜์ง€ ์•Š์•„์„œ ๊ฐœ๋ฐœ์ž์— ์˜ํ•ด ์–ธ์ œ๋“  ์ˆ˜์ •๋  ์ˆ˜ ์žˆ์Œ.

Bmw.prototype = {
  constructor: Bmw, // ์ถ”๊ฐ€๋กœ ์ ์€ ๊ฒƒ
  wheels: 4,
  drive() {
    console.log("drive..");
  },
};
// ๐ŸŽต closure๋ฅผ ์‚ฌ์šฉํ•ด ์ดˆ๊ธฐ์— ์„ค์ •ํ•œ color ๊ฐ’์„ ๋ฐ›์„ ์ˆ˜๋งŒ ์žˆ๊ณ  ๋ณ€๊ฒฝ์€ ๋ชป ํ•˜๊ฒŒ ํ•˜๊ธฐ
const Bmw = function (color) {
  this.color = color;
};

const x5 = new Bmw("red");
// ์œ„ code๋Š” x5.color = "black" ๋“ฑ์œผ๋กœ color๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Œ

// โ†“ closure๋ฅผ ์จ์„œ ๋ณ€๊ฒฝํ•˜๋ฉด

const Bmw = function (color) {
  const c = color;
  this.getColor = function () {
    console.log(c);
  };
};

const x5 = new Bmw("red"); //getColor ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑ๋  ๋‹น์‹œ์˜ context ๊ธฐ์–ตํ•˜๊ณ  ์žˆ์Œ

15. class

  • class
    • ์ƒ์„ฑ์ž ํ•จ์ˆ˜์ฒ˜๋Ÿผ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Œ
    • ์ƒ์„ฑ์ž ํ•จ์ˆ˜์™€ ๋‹ฌ๋ฆฌ new๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š์•˜์„ ๋•Œ "undefined"์— ๊ทธ์น˜๋Š” ๊ฒŒ ์•„๋‹Œ, "TypeError"๊ฐ€ ๋ฐœ์ƒํ•จ (error๋ฅผ ์ธ์ง€ํ•  ์ˆ˜ ์žˆ์Œ)
    • ์ƒ์„ฑ์ž ํ•จ์ˆ˜์™€ ๋‹ฌ๋ฆฌ constructor์— class๋ผ๋Š” ๊ฒŒ ๋ช…์‹œ๋จ
    • for ... in๋ฌธ์—์„œ ์ œ์™ธ๋จ
    • ์ƒ์† ์‹œ extends keyword๋ฅผ ์‚ฌ์šฉํ•จ
    • ES6์—์„œ ์ถ”๊ฐ€๋จ
// ๐ŸŽต class๋กœ car๋ฅผ ๋งŒ๋“ค๊ณ  extends๋กœ bmw์— ์ƒ์†ํ•˜๊ธฐ
class Car {
  constructor(color) {
    this.color = color;
    this.wheels = 4;
  }
  drive() {
    console.log("drive..");
  }
  stop() {
    console.log("STOP!");
  }
}

class Bmw extends Car {
  park() {
    console.log("PARK");
  }
}

const z4 = new Bmw("blue"); // __proto__์— f park()๊ฐ€ ์žˆ๊ณ , __proto__์˜ __proto__์— drive์™€ stop์ด ์žˆ์Œ
  • method overriding
    • super
      • ๋งŒ์•ฝ Bmw์— Car์—์„œ ์ •์˜ํ•œ method์™€ ๋™์ผํ•œ method๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด, Bmw์˜ method๊ฐ€ ๋‚˜์˜ด (Bmw์˜ method๊ฐ€ Car์˜ method๋ฅผ ๋ฎ์–ด์“ด ๊ฒƒ)
      • ์ด๋ ‡๊ฒŒ ๋™์ผํ•œ method๊ฐ€ ์žˆ์„ ๋•Œ, Bmw์˜ ๊ฒƒ์ด Car์˜ ๊ฒƒ์„ ๋ฎ์–ด์“ฐ๋Š” ๊ฒŒ ์•„๋‹Œ, ํ•ฉ์ณ์„œ ํ™•์žฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด super๋ฅผ ์‚ฌ์šฉ
// ๐ŸŽต ์ž์‹ class์— ์žˆ๋Š” ํ•จ์ˆ˜๋„ ์‚ฌ์šฉํ•˜๊ณ , ๋ถ€๋ชจ class์— ์žˆ๋Š” ํ•จ์ˆ˜๋„ ์‚ฌ์šฉํ•˜๊ธฐ
class Car {
  constructor(color) {
    this.color = color;
    this.wheels = 4;
  }
  drive() {
    console.log("drive..");
  }
  stop() {
    console.log("STOP!");
  }
}

class Bmw extends Car {
  park() {
    console.log("PARK");
  }
  stop() {
    super.stop(); // ๋ถ€๋ชจ class์ธ Car์˜ stopํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ
    console.log("OFF");;
  }
}

const z4 = new Bmw("blue");
  • constructor overriding
class Car {
  constructor(color) {
    this.color = color;
    this.wheels = 4;
  }
  drive() {
    console.log("drive..");
  }
  stop() {
    console.log("STOP!");
  }
}

class Bmw extends Car {
  constructor(color) { // ์ž์‹ ์ƒ์„ฑ์ž๋Š” ๋ถ€๋ชจ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•˜๋ฏ€๋กœ constructor์™€ super์„ ์ ์–ด์„œ this.property๋กœ ํ• ๋‹นํ•ด ์ฃผ์–ด์•ผ ํ•จ, constructor์™€ super์— color๋ฅผ ๋ฐ›์•„ Car์— ๋„˜๊ฒจ์ฃผ์–ด์•ผ z4์˜ color ์ •๋ณด๊นŒ์ง€ ์ž˜ ๋‚˜์˜ด
    super(color);
  }
  park() {
    console.log("PARK");
  }
}

const z4 = new Bmw("blue");

16. promise

// ๐ŸŽต 3์ดˆ ํ›„ ๊ฒฐ๊ณผ์— ๋”ฐ๋ฅธ ํ•จ์ˆ˜ ๋ณด์—ฌ์ฃผ๊ธฐ
const pr = new Promsie ((resolve, reject) => {
  setTimeout(() => {
    resolve("OK")
  }, 3000)
});

pr.then(
  function (result) {}, // ์ดํ–‰๋˜์—ˆ์„ ๋•Œ ์‹คํ–‰
  function (reject) {} // ๊ฑฐ๋ถ€๋˜์—ˆ์„ ๋•Œ ์‹คํ–‰
  finally () {} // ์ดํ–‰์ด๋“  ๊ฑฐ๋ถ€๋œ ์ฒ˜๋ฆฌ๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด ์‹คํ–‰
);

// โ†“ ์•„๋ž˜์™€ ๊ฐ™์ด ์ ๋Š” ๊ฒŒ ๊ฐ€๋…์„ฑ์— ์ข‹๊ณ , ์ฒซ ๋ฒˆ์งธ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ–ˆ๋‹ค๊ฐ€ ๋‚˜๋Š” error๋„ ์žก๊ธฐ ์‰ฌ์›€

const pr = new Promise ((resolve, reject) => {
  setTimeout(() => {
    resolve("OK")
  }, 3000)
});

pr.then(
  function (result) {},
).catch(
  function (reject) {},
).finally(
  function () {
    console.log("--- ์ฃผ๋ฌธ ๋ ---") // loading ํ™”๋ฉด๊ฐ™์€ ๊ฑธ ์—†์•จ ๋•Œ ์œ ์šฉํ•จ
  }
)
// ๐ŸŽต 1๋ฒˆ๋ถ€ํ„ฐ 3๋ฒˆ๊นŒ์ง€ ์ˆœ์„œ๋Œ€๋กœ ์ œํ’ˆ ์ฃผ๋ฌธํ•˜๊ธฐ
// (1) callback hell ํ˜•์‹์œผ๋กœ ์ž‘์„ฑ
const f1 = (callback) => {
  setTimeout(function () {
    console.log("1๋ฒˆ ์ฃผ๋ฌธ ์™„๋ฃŒ");
    callback();
  }, 1000);
};

const f2 = (callback) => {
  setTimeout(function () {
    console.log("2๋ฒˆ ์ฃผ๋ฌธ ์™„๋ฃŒ");
    callback();
  }, 3000);
};

const f3 = (callback) => {
  setTimeout(function () {
    console.log("3๋ฒˆ ์ฃผ๋ฌธ ์™„๋ฃŒ");
    callback();
  }, 2000);
};

console.log("์‹œ์ž‘");
f1(function () {
  f2(function () {
    f3(function () {
      console.log("๋");
    });
  });
});

// (2) promise chaining ํ˜•์‹์œผ๋กœ ์ž‘์„ฑ (์•ฝ 6์ดˆ ์ •๋„ ๊ฑธ๋ฆผ)
const f1 = () => {
  return new Promise((res, rej) => {
    setTimeout(() => {
      res("1๋ฒˆ ์ฃผ๋ฌธ ์™„๋ฃŒ");
    }, 1000);
  });
};

const f2 = (message) => {
  console.log(message);
  return new Promise((res, rej) => {
    setTimeout(() => {
      res("2๋ฒˆ ์ฃผ๋ฌธ ์™„๋ฃŒ");
    }, 3000);
  });
};

const f3 = (message) => {
  console.log(message);
  return new Promise((res, rej) => {
    setTimeout(() => {
      res("3๋ฒˆ ์ฃผ๋ฌธ ์™„๋ฃŒ");
    }, 2000);
  });
};

console.log("์‹œ์ž‘");
f1()
  .then((res) => f2(res))
  .then((res) => f3(res))
  .then((res) => console.log(res))
  .catch(console.log)
  .finally(() => {
    console.log("๋");
});

// (3) Promise.all์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์„ฑ (์„ธ ์ œํ’ˆ์ด ๋™์‹œ์— ์ฃผ๋ฌธ๋˜๋ฉด ๊ฐ€์žฅ ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ์ œํ’ˆ์„ ๋ดค์„ ๋•Œ ์ตœ๋Œ€ 3์ดˆ๊ฐ€ ๊ฑธ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ๊ณผ๊ฐ€ ๋นจ๋ฆฌ ๋„์ถœ๋จ)
const f1 = () => {
  return new Promise((res, rej) => {
    setTimeout(() => {
      res("1๋ฒˆ ์ฃผ๋ฌธ ์™„๋ฃŒ");
    }, 1000);
  });
};

const f2 = (message) => {
  console.log(message);
  return new Promise((res, rej) => {
    setTimeout(() => {
      res("2๋ฒˆ ์ฃผ๋ฌธ ์™„๋ฃŒ");
    }, 3000);
  });
};

const f3 = (message) => {
  console.log(message);
  return new Promise((res, rej) => {
    setTimeout(() => {
      res("3๋ฒˆ ์ฃผ๋ฌธ ์™„๋ฃŒ");
    }, 2000);
  });
};

console.time("x"); // ์•„๋ž˜ console.timeEnd()์™€ ๊ฐ™์ด ์†Œ์š” ์‹œ๊ฐ„์„ ์ธก์ •ํ•  ์ˆ˜ ์žˆ์Œ
Promise.all([f1(), f2(), f3()]) // f1(), f2(), f3()๊ฐ€ ๋ชจ๋‘ ์™„๋ฃŒ๋˜์–ด์•ผ then ๋ถ€๋ถ„์ด ์‹คํ–‰๋จ
.then(res => {
  console.log(res);
  console.timeEnd("x");
});
  • Promise.all()
    • ํ•˜๋‚˜๋ผ๋„ reject๊ฐ€ ์žˆ์œผ๋ฉด ์ „์ฒด๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š์Œ
    • ํ•˜๋‚˜์˜ ์ •๋ณด๋ผ๋„ ๋ˆ„๋ฝ๋˜๋ฉด ์ „์ฒด๋ฅผ ๋ณด์—ฌ์ฃผ์ง€ ์•Š์•„์•ผ ํ•  ๋•Œ ์‚ฌ์šฉ (๋‹ค ๋ณด์—ฌ์ฃผ๊ฑฐ๋‚˜, ํ•˜๋‚˜๋„ ๋ณด์—ฌ์ฃผ์ง€ ์•Š๊ฑฐ๋‚˜)
  • Promise.race()
    • Promise.all()์€ ๋ชจ๋“  ์ž‘์—…์ด ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ์ง€๋งŒ, Promise.race()๋Š” ํ•˜๋‚˜๋ผ๋„ 1๋“ฑ์œผ๋กœ ์™„๋ฃŒ๋˜๋ฉด ๋๋ƒ„
    • ์šฉ๋Ÿ‰์ด ํฐ image๋“ค์„ loadingํ•˜๋Š”๋ฐ ๊ทธ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์™„๋ฃŒ๋˜๋ฉด ๊ทธ image๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ
// ์œ„ ์˜ˆ์ œ์— ์ด์–ด์„œ
// (4)
Promise.race([f1(), f2(), f3()]) // f1(), f2(), f3()๊ฐ€ ๋ชจ๋‘ ์™„๋ฃŒ๋˜์–ด์•ผ then ๋ถ€๋ถ„์ด ์‹คํ–‰๋จ
.then(res => {
  console.log(res);
});

17. async, await

async๋‚˜ away๋ฅผ ํ†ตํ•ด promise ํ•จ์ˆ˜๋ฅผ chain์œผ๋กœ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๊ฐ€๋…์„ฑ์ด ์ข‹์•„์ง„๋‹ค.

  • async : function ์•ž์— ๋ถ™์—ฌ์ฃผ๋ฉด ํ•ญ์ƒ promise๋ฅผ ๋ฐ˜ํ™˜ํ•จ
// (1)
async function getName () {
  return "Mai";
}

console.log(getName()); // Promise { 'Mai' }
getName().then((name) => {
  console.log(name); // "Mai"
});

// (2)
async function getName () {
  return Promise.resolve("Mai");
}

getName().then((name) => {
  console.log(name); // "Mai"
});

// (3)
async function getName () {
  throw new Error("err...");
}

getName().catch((err) => {
  console.log(err); // Error: 'err...'
});
  • await
    • ํ•ญ์ƒ async ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ (๋‹ค๋ฅธ ๋ฐ์„  error ๋ฐœ์ƒํ•จ)
    • await ํ•จ์ˆ˜ ์˜†์— Promise๊ฐ€ ์˜ค๊ณ , ์ด๊ฒŒ ๋‹ค ์ฒ˜๋ฆฌ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆผ
    • Promise ... then๋ณด๋‹ค await๋กœ ์“ฐ๋Š” ๊ฒŒ ๊ฐ€๋…์„ฑ์ด ๋” ์ข‹์Œ
// ๐ŸŽต 1์ดˆ ํ›„์— "Mai"๊ฐ€ ์ฐํž˜
function getName(name) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(name);
    }, 1000);
  });
}

async function showName() {
  const result = await getName("Mai");
  console.log(result);
}

console.log("์‹œ์ž‘");
showName();
// ๐ŸŽต 1๋ฒˆ๋ถ€ํ„ฐ 3๋ฒˆ๊นŒ์ง€ ์ˆœ์„œ๋Œ€๋กœ ์ œํ’ˆ ์ฃผ๋ฌธํ•˜๊ธฐ
// (5) await, async๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์„ฑ
const f1 = () => {
  return new Promise((res, rej) => {
    setTimeout(() => {
      res("1๋ฒˆ ์ฃผ๋ฌธ ์™„๋ฃŒ");
    }, 1000);
  });
};

const f2 = (message) => {
  console.log(message);
  return new Promise((res, rej) => {
    setTimeout(() => {
      res("2๋ฒˆ ์ฃผ๋ฌธ ์™„๋ฃŒ");
      // rej(new Error("err..."));
    }, 3000);
  });
};

const f3 = (message) => {
  console.log(message);
  return new Promise((res, rej) => {
    setTimeout(() => {
      res("3๋ฒˆ ์ฃผ๋ฌธ ์™„๋ฃŒ");
    }, 2000);
  });
};

console.log("์‹œ์ž‘");
async function order() {
  try { // async await๋ฅผ ์“ธ ๋•Œ ์˜ค๋ฅ˜๋ฅผ ๋Œ€๋น„ํ•ด ๊ฐ์‹ธ์ฃผ๋Š” try ... catch๋ฌธ
    const result1 = await f1();
    const result2 = await f2(result1);
    const result3 = await f3(result2);
    console.log(result3);
  } catch (e) {
    console.log(e); // ์ด ๋ถ€๋ถ„์—์„œ ์ ์ ˆํ•œ error๋ฅผ ์ฒ˜๋ฆฌํ•ด์ฃผ๊ณ  ๋„˜์–ด๊ฐ€๋ฉด ๋จ
  }
  console.log("์ข…๋ฃŒ");
}
order();

// Promise.all๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๊ธฐ
// ์œ„ ์˜ˆ์ œ์— ์ด์–ด์„œ
console.log("์‹œ์ž‘");
async function order() {
  try {
    const result = await Promise.all([f1(), f2(), f3()]);
    console.log(result);
  } catch (e) {
    console.log(e);
  }
  console.log("์ข…๋ฃŒ");
}
order(); // [ '1๋ฒˆ ์ฃผ๋ฌธ ์™„๋ฃŒ', '2๋ฒˆ ์ฃผ๋ฌธ ์™„๋ฃŒ', '3๋ฒˆ ์ฃผ๋ฌธ ์™„๋ฃŒ' ]

18. generator

  • generator ๊ฐ์ฒด
    • ํ•จ์ˆ˜์˜ ์‹คํ–‰์„ ์ค‘๊ฐ„์— ๋ฉˆ์ท„๋‹ค๊ฐ€ ์žฌ๊ฐœํ•  ์ˆ˜ ์žˆ์Œ
      • ๋‹ค๋ฅธ ์ž‘์—…์„ ํ•˜๋‹ค๊ฐ€ ๋‹ค์‹œ ๋Œ์•„์™€์„œ next() ํ•ด์ฃผ๋ฉด ์ง„ํ–‰์ด ๋ฉˆ์ท„๋˜ ๋ถ€๋ถ„๋ถ€ํ„ฐ ์ด์–ด์„œ ์‹คํ–‰
      • Redux Saga์—์„œ ๋งŽ์ด ์“ฐ์ž„
    • function ๋ฐ”๋กœ ์˜†์— '*'๋ฅผ ๋ถ™์ž„
    • yield์—์„œ ํ•จ์ˆ˜์˜ ์‚ฌ์šฉ์„ ๋ฉˆ์ถœ ์ˆ˜ ์žˆ์Œ
    • next() method๊ฐ€ ์žˆ์œผ๋ฉฐ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด yield๊นŒ์ง€ ๊ฐ€๊ณ  ๋ฉˆ์ถค
    • value์™€ done์ด๋ผ๋Š” property๋ฅผ ๊ฐ€์ง
      • value๋Š” yield ์˜ค๋ฅธ์ชฝ ๊ฐ’์ž„ (๊ฐ’์„ ์ƒ๋žตํ•˜๋ฉด "undefined"์ž„)
      • done : ํ•จ์ˆ˜๊ฐ€ ๋๋‚ฌ๋Š”์ง€ ๋‚˜ํƒ€๋‚ด๋ฉฐ ์‹คํ–‰์ด ๋๋‚ฌ์œผ๋ฉด "true", ์•ˆ ๋๋‚ฌ์œผ๋ฉด "false"์ž„
    • next() ์™ธ์— return()๊ณผ throw() method๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Œ
      • ํ•จ์ˆ˜๊ฐ€ ๋๋‚˜๊ธฐ ์ „์— return() method๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด done์ด true๊ฐ€ ๋˜๋ฉฐ, ๊ทธ ์ดํ›„์— next method๋ฅผ ์‹คํ–‰ํ•ด๋„ ๊ณ„์† value๊ฐ€ undefine, done์ด true๊ฐ€ ๋จ
    • ๋ฐ˜๋ณต์ด ๊ฐ€๋Šฅํ•จ (iterable)
    • Symbol.iterator method๊ฐ€ ๊ตฌํ˜„๋˜์–ด ์žˆ์–ด์•ผ ํ•จ
      • ์ด method๋กœ ํ˜ธ์ถœํ•œ ๊ฒฐ๊ณผ๊ฐ€ iterator์ธ๋ฐ, ์ด iterator๋Š” value์™€ done์ด๋ผ๋Š” property๋ฅผ ๊ฐ€์ง€๋Š” ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” method์ธ next๊ฐ€ ์žˆ์–ด์•ผ ํ•จ
    • generator๋Š” ๊ฐ’์„ ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด๋‘์ง€ ์•Š๊ณ  ํ•„์š”ํ•œ ๊ฐ’๋งŒ ๊ทธ๋•Œ๋„๋•Œ ์ƒ์„ฑํ•จ
      • ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜๋กœ๋Š ์–ด๋–ค ๊ฐ’์„ ๊ตฌํ•  ๋•Œ ๋ชจ๋“  ๊ฐ’์„ ๋ฏธ๋ฆฌ ๊ณ„์‚ฐํ•ด ๋†”์•ผ ํ•˜๊ณ , ์“ธ์ง€ ์•ˆ ์“ธ์ง€ ์ •ํ•ด์ง€์ง€ ์•Š์€ ์ƒํ™ฉ์—์„œ๋„ ๊ทธ ๊ฐ’์„ ์œ ์ง€ํ•ด์•ผ ํ•จ
      • ํ•„์š”ํ•œ ์ˆœ๊ฐ„๊นŒ์ง€ ๊ณ„์‚ฐ์„ ๋ฏธ๋ฃจ๊ณ , ํ•„์š”ํ•  ๋•Œ ์—ฐ์‚ฐํ•ด์„œ ๊ฐ’์„ ์ฃผ๊ธฐ ๋•Œ๋ฌธ์— memory ๊ด€๋ฆฌ ์ธก๋ฉด์—์„œ ํšจ์œจ์ ์ž„
      • while (true)๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด์„œ break๋ฅผ ์“ฐ์ง€ ์•Š์•„๋„ ๋จ
  • โœ”๏ธ ๋ฐฐ์—ด์˜ ๋ฐ˜๋ณต์„ฑ
    • ๋ฐฐ์—ด๋„ prototype์„ ๋ณด๋ฉด "Symbol(Symbol.iterator)"๊ฐ€ ์žˆ์Œ
    • ๋ฐฐ์—ด์€ Symbol.iterator method๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ์ด method๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ’์ด iterator์ž„์ด๋ฏ€๋กœ ๋ฐฐ์—ด์€ iterable (๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ) ๊ฐ์ฒด์ž„
    • for ... of๋ฌธ์œผ๋กœ ์ˆœํšŒํ•  ์ˆ˜ ์žˆ์Œ
// ๐ŸŽต it๋ผ๋Š” ๋ณ€์ˆ˜ ์•ˆ์— arr๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” Symbol.iterator method๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ
const arr = [1, 2, 3, 4, 5];

const it = arr[Symbol.iterator]();

it.next(); // {value: 1, done: false} ...

// ๐ŸŽต for ... of๋ฌธ์œผ๋กœ ์ˆœํšŒํ•˜๊ธฐ
for (let num of arr) {
  console.log(num)
};
// ๐ŸŽต
function* fn() {
  yield 4;
  yield 5;
  yield 6;
}

const a = fn();

a[Symbol.iterator]() === a; // true : a๋ผ๋Š” generator์— Symbol.iterator๋ผ๋Š” method๋ฅผ ์‹คํ–‰ํ•œ ๊ฐ’์ด ์ž๊ธฐ ์ž์‹ ๊ณผ ๊ฐ™๋‹ค๋Š” ๋ง

for (let num of a) { // for ... of๊ฐ€ ์‹œ์ž‘๋˜๋ฉด a[Symbol.iterator]()๋ฅผ ํ˜ธ์ถœํ•˜๊ณ   ์ด๊ฒŒ ์—†์œผ๋ฉด error๊ฐ€ ๋ฐœ์ƒํ•œ ํ›„ ๋ฐ˜ํ™˜๋œ iterator์— next method๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ done์ด true๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•จ
  console.log(num);
} // 4, 5, 6
// ๐ŸŽต next() method์— ์ธ์ˆ˜ ์ „๋‹ฌํ•˜๊ธฐ
function* fn() {
  const num1 = yield "์ฒซ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.";
  console.log(num1);
  
  const num2 = yield "๋‘ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.";
  console.log(num2);
  
  return num1 + num2;
}

const a = fn();

a.next() // {value: "์ฒซ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.", done: false}
a.next(4) // ์ธ์ˆ˜๊ฐ€ num1์— ์ €์žฅ๋จ, {value: "๋‘ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.", done: false}
a.next(7) // ์ธ์ˆ˜๊ฐ€ num2์— ์ €์žฅ๋จ, {value: 7, done: true}
// ๐ŸŽต ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•ด๋„ ๋ฌด๋ฆฌ ๊ฐ€์ง€ ์•Š๋Š” generator
function* fn() {
  let index = 0;
  while (true) {
    yield index++;
  }
}

const a = fn(); // a.next();๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋งŒ ์—ฐ์‚ฐ๋ผ์„œ break๊ฐ€ ์—†๋Š” ๋ฌดํ•œ ๋ฐ˜๋ณต๋ฌธ์ž„์—๋„ ๋ป—์ง€ ์•Š์Œ
// ๐ŸŽต yield๋กœ ๋‹ค๋ฅธ generator ๋ถ€๋ฅด๊ธฐ
function* gen1() {
  yield "W";
  yield "O";
  yield "R";
  yield "L";
  yield "D";
}

function* gen2() {
  yield "Hello,";
  yield* gen1();
  yield "!";
}

console.log(...gen2()); // "Hello," "W" "O" "R" "L" "D" "!"




Endnote

๐Ÿ”— related documents

Lodash

๐Ÿ™‡ the source of this content

์ฝ”๋”ฉ์•™๋งˆ

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ