[TIL] 210904

Lee SyongΒ·2021λ…„ 9μ›” 4일
0

TIL

λͺ©λ‘ 보기
17/204
post-thumbnail

πŸ“ 였늘 ν•œ 것

  1. 콜백 ν•¨μˆ˜ / ν™”μ‚΄ν‘œ ν•¨μˆ˜ / class μ„ μ–Έ / object 생성 / class 속성 / 상속 & λ‹€μ–‘μ„± ν•™μŠ΅

  2. μžλ°”μŠ€ν¬λ¦½νŠΈ κ°•μ˜ μˆ˜κ°• (ν•¨μˆ˜, class)

  3. λ¬Έμžμ—΄ λ©”μ„œλ“œ ν•™μŠ΅ - [TIL] 210901 μ°Έκ³ 


πŸ“– ν•™μŠ΅ 자료

  1. λ“œλ¦Όμ½”λ”© 유튜브 'μžλ°”μŠ€ν¬λ¦½νŠΈ' κ°•μ˜ 5, 6편

  2. μ½”λ”©μ• ν”Œ 유튜브 '객체지ν–₯ Class 문법 10λΆ„λ§Œμ— μ΄ν•΄μ‹œμΌœμ€Œ (μžλ°”μŠ€ν¬λ¦½νŠΈ)'

  3. 사전 ν•™μŠ΅ κ°€μ΄λ“œ STEP 2 string λ¬Έμžμ—΄ λ©”μ„œλ“œ


πŸ“š 배운 것

1. ν•¨μˆ˜ ν‘œν˜„μ‹

1) 콜백 ν•¨μˆ˜(callback function)

λ§€κ°œλ³€μˆ˜μ— μ •μ˜λœ ν•¨μˆ˜λ“€ 쀑 상황에 λ§žλŠ” ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜λŠ” ν•¨μˆ˜

const printYes = function () {
  console.log('Yes');
}
// 1. printYesλΌλŠ” λ³€μˆ˜μ— ν•¨μˆ˜ ν• λ‹Ή

const printNo = function hey () {
  console.log('No');
}
// 2. printNoλΌλŠ” λ³€μˆ˜μ— ν•¨μˆ˜ ν• λ‹Ή

function random(answer, printYes, printNo) {
  if (answer === 'happy') {
    printYes();
  } else {
    printNo();
  }
}
// 콜백 ν•¨μˆ˜ μ •μ˜

random('happy', printYes, printNo); // No
// 인수λ₯Ό λ§€κ°œλ³€μˆ˜μ— μ „λ‹¬ν•΄μ£Όλ©΄μ„œ ν•¨μˆ˜ 호좜

주의 ❗

  • 콜백 ν•¨μˆ˜μ—μ„œ (answer, printYes, printNo)λŠ” κ·Έ 자체둜 ν•¨μˆ˜ 이름이 μ•„λ‹ˆλΌ ν•¨μˆ˜κ°€ ν• λ‹Ήλœ λ³€μˆ˜μ΄λ‹€. (1, 2. μ½”λ“œ 지 λ•Œ μ£Όμ˜ν•  것)
  • 2.μ—μ„œ ν•¨μˆ˜ 이름을 μ§€μ •ν•œ μ΄μœ λŠ” 디버깅 ν•  λ•Œ μŠ€νƒ 좔적(stack trace)에 ν•¨μˆ˜ 이름이 λ‚˜μ˜€λ„λ‘ ν•˜κΈ° μœ„ν•¨μ΄λ‹€.

2) ν™”μ‚΄ν‘œ ν•¨μˆ˜(arrow function)

  • κ°„κ²°ν•˜κ²Œ ν‘œν˜„ν•œ ν•¨μˆ˜

  • function ν‚€μ›Œλ“œμ™€ { }을 λΉΌκ³ , ν•œ 쀄에 κ°„κ²°ν•˜κ²Œ 적을 수 μžˆλ‹€

const simplePrint1 = function () {
  console.log('simplePrint1');
}
// 이걸 ν™”μ‚΄ν‘œ ν•¨μˆ˜λ‘œ ν‘œν˜„ν•˜λ©΄

const simplePrint2 = () => console.log('simplePrint2');
const add1 = function (a, b) {
  return a + b;
}
// 이걸 ν™”μ‚΄ν‘œ ν•¨μˆ˜λ‘œ ν‘œν˜„ν•˜λ©΄

const add2 = (a, b) => a + b;
  • ν•œ 쀄 μ΄μƒμ˜ λ³΅μž‘ν•œ ν•¨μˆ˜λΌλ©΄ { }을 μ‚¬μš©ν•  μˆ˜λ„ μžˆλ‹€.
const simpleMultiply = (a, b) => {
  // do somthing more
  return a + b;
};

cf. IIFE (μ¦‰μ‹œ μ‹€ν–‰ ν•¨μˆ˜ ν‘œν˜„)

  • μ›λž˜λŠ” ν•¨μˆ˜λ₯Ό μ„ μ–Έν•˜κ³  κ·Έ 밑에 foo(); λΌλŠ” μ‹μœΌλ‘œ ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•΄μ€˜μ•Ό ν•œλ‹€

  • κ·ΈλŸ¬λ‚˜ κ΄„ν˜Έλ₯Ό 2번 μ‚¬μš©ν•΄ ν•¨μˆ˜λ₯Ό 선언함과 λ™μ‹œμ— ν˜ΈμΆœν•  수 μžˆλ‹€.

(function IIFE () {
  console.log('IIFE');
})(); // IIFE

cf. ν€΄μ¦ˆ

  1. μž…λ ₯받은 command에 따라 a와 b의 숫자 값을 λ”ν•˜κ±°λ‚˜ λΉΌκ±°λ‚˜ λ‚˜λˆ„κ±°λ‚˜ κ³±ν•˜λŠ” ν•¨μˆ˜λ₯Ό λ§Œλ“€μ–΄λΌ (ν•¨μˆ˜ 이름 calculate)
  2. commandλŠ” add, substract, divide, multiply, reminder μ€‘μ—μ„œ λ°›μ•„μ„œ μ²˜λ¦¬ν•  것
  3. λ‹€λ₯Έ commandκ°€ λ“€μ–΄μ˜€λ©΄ 그건 μ–΄λ–»κ²Œ μ²˜λ¦¬ν• μ§€λ„ 생각해볼 것
function calculate (command, a, b) { // 1을 κ΅¬ν˜„
  switch (command) {
    case 'add': // 2λ₯Ό κ΅¬ν˜„
      return a + b;
    case 'substract':
      return a - b;
    case 'divide':
      return a / b;
    case 'multiply':
      return a * b;
    case 'reminder':
      return a % b;
    default: // 3을 κ΅¬ν˜„
      throw Error('unknown command');
  }
}

console.log(calculate(`add`, 2, 3)); // 5

2. class VS object

πŸ’‘ class

  • λ°μ΄ν„°λŠ” λ“€μ–΄ μžˆμ§€ μ•Šκ³ , 이런 데이터듀이 λ“€μ–΄κ°ˆ 수 μžˆλ‹€κ³  ν‹€λ§Œ μž‘μ•„λ†“μ€ 것
  • ν•œ 번만 μ„ μ–Έν•΄μ€€λ‹€
  • μ •μ˜λ§Œ ν•œ 것이기에 μ‹€μ œλ‘œ λ©”λͺ¨λ¦¬μ— ν• λ‹Ήλ˜μ§€λŠ” μ•ŠλŠ”λ‹€

πŸ’‘ object

  • class에 데이터λ₯Ό 넣은 것
  • classλ₯Ό μ΄μš©ν•΄ μ—¬λŸ¬ 번 생성 κ°€λŠ₯
  • μ‹€μ œλ‘œ λ©”λͺ¨λ¦¬μ— ν• λ‹Ήλœλ‹€

1) classλž€?

  • κ΄€λ ¨ μžˆλŠ” λ³€μˆ˜λ‚˜ ν•¨μˆ˜λ“€μ„ 묢어놓은 것

    • λ³€μˆ˜ : 속성(μƒνƒœ) : fields
    • ν•¨μˆ˜ : 행동 : methods
  • λΉ„μŠ·ν•œ objectλ₯Ό 많이 λ§Œλ“€ 일이 μžˆμ„ λ•Œ, classλ₯Ό μ„ μ–Έν•˜λ©΄ objectλ₯Ό μ‰½κ²Œ 많이 생성할 수 μžˆλ‹€.

  • 데이터 class : 속성(fields)은 μ—†κ³  행동(methods)만 μžˆλŠ” class

  • μΊ‘μŠν™”(encapsulation) : λ‚΄λΆ€μ μœΌλ‘œλ§Œ λ³Ό 수 μžˆλŠ” λ³€μˆ˜μ™€ λ°”κΉ₯μ—μ„œλ„ λ³Ό 수 μžˆλŠ” λ³€μˆ˜λ‘œ λ‚˜λˆˆλ‹€

  • classλ₯Ό μ΄μš©ν•΄ 상속과 닀양성이 일어날 수 μžˆλ‹€

β†’ 이 λͺ¨λ“  것듀은 javascriptκ°€ 객체 지ν–₯ 언어이기 λ•Œλ¬Έμ— κ°€λŠ₯ν•œ 것이닀


2) class μ„ μ–Έ

class Person {
  // μƒμ„±μž(constructor)
  constructor(name, age) {
    // 속성(fields)
    this.name = name; // μƒˆλ‘œ μƒμ„±λ˜λŠ” object에 {name: name}을 좔가해라
    this.age = age;
  }

  //행동(mothods)
  speak() {
    console.log(`${this.name}: hello!`);
  }
}

κ°œλ… 정리 ❗

  • class 이름 : κ΄€μŠ΅μ μœΌλ‘œ λŒ€λ¬Έμž
  • μƒμ„±μž(constructor) : objectλ₯Ό λ§Œλ“€ λ•Œ ν•„μš”ν•œ 데이터가 전달됨
  • this : PersonμœΌλ‘œλΆ€ν„° μƒμ„±λ˜λŠ” object (즉, instance)
  • 속성(fields), 행동(methods) : μ „λ‹¬λœ 데이터가 할당됨

3) object 생성

const honey = new Person('syong', 20);

// μœ„μ™€ μ•„λž˜λŠ” 같은 λ‚΄μš©μ΄λ‹€. μ›λž˜ 4쀄 μ“Έ 것을 1쀄에 담을 수 μžˆλ‹€.

const honey = {
  name : 'syong',
  age : 20
}

console.log(honey.name); // syong
console.log(honey.age); // 20 honey.speak(); // syong: hello!
honey.speak(); // syong: hello!

κ°œλ… 정리 ❗

  • μƒˆλ‘œμš΄ objectλ₯Ό λ§Œλ“€ λ•Œ newλΌλŠ” ν‚€μ›Œλ“œ μ‚¬μš©

3. μ ‘κ·Όμž ν”„λ‘œνΌν‹° πŸ™‹β€β™€οΈπŸ’’πŸ’’

객체가 가진 ν”„λ‘œνΌν‹° 값을, 객체 λ°”κΉ₯μ—μ„œ μ½κ±°λ‚˜ μ“°κΈ° μœ„ν•΄ μ‚¬μš©λ¨

1) get

  • ν”„λ‘œνΌν‹°λ₯Ό 읽고자 ν•  λ•Œ μ‹€ν–‰λœλ‹€

  • getμ΄λΌλŠ” ν‚€μ›Œλ“œλ₯Ό μ΄μš©ν•΄ 값을 return ν•  수 μžˆλ‹€

2) set

  • ν”„λ‘œνΌν‹°μ— 값을 ν• λ‹Ήν•˜κ³ μž ν•  λ•Œ μ‹€ν–‰λœλ‹€

  • setμ΄λΌλŠ” ν‚€μ›Œλ“œλ₯Ό μ΄μš©ν•΄ 값을 μ„€μ •ν•˜λ―€λ‘œ 'value'λ₯Ό 받아와야 ν•œλ‹€


ex) age 값이 -1인 경우, console 창에 λ‹€λ₯Έ 값을 λ„μ›Œμ€€λ‹€

class User {
  constructor(firstName, lastName, age) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
  }


  // getter
  get age() {
    return this._age;
    // get age()λ₯Ό ν˜ΈμΆœν•˜λ©΄ this.ageλ₯Ό return ν•˜κ²Œ λœλ‹€
  }


  // setter
  set age(value) {
  if (value <0) {
    throw Error('λ‚˜μ΄λŠ” 음수일 수 μ—†λ‹€')
  } 
    this._age = value;
    // μƒˆλ‘œμš΄ valueλ₯Ό λ°›μœΌλ©΄ κ·Έ valueλ₯Ό this.age에 ν• λ‹Ήν•˜κ²Œ λœλ‹€
  }
}


const user1 = new User('Steve', 'Jobs', -1);
console.log(user1.age); // 'λ‚˜μ΄λŠ” 음수일 수 μ—†λ‹€'λŠ” 문ꡬ의 errorκ°€ 뜸

β€» setter μ˜μ—­μ—μ„œ if 쑰건문을 μ§€μš°κ³ , this._age = value;λ₯Ό this._age = value < 0 ? 0 : value;둜 λ°”κΏ€ μˆ˜λ„ μžˆλ‹€

주의 ❗

getter, setter μ•ˆμ˜ λ³€μˆ˜λŠ” age라고 λ˜‘κ°™μ΄ μ“°λ©΄ μ•ˆλœλ‹€

  1. age()λΌλŠ” getterλ₯Ό μ •μ˜ν•˜λŠ” μˆœκ°„, this.ageλŠ” λ©”λͺ¨λ¦¬μ— μžˆλŠ” 데이터λ₯Ό μ½μ–΄μ˜€λŠ” 게 μ•„λ‹ˆλΌ getterλ₯Ό ν˜ΈμΆœν•˜κ²Œ λœλ‹€

  2. age()λΌλŠ” setterλ₯Ό μ •μ˜ν•˜λŠ” μˆœκ°„, 값을 ν• λ‹Ήν•  λ•Œ λ°”λ‘œ λ©”λͺ¨λ¦¬μ— μžˆλŠ” 값을 ν• λ‹Ήν•˜λŠ” 게 μ•„λ‹ˆλΌ setterλ₯Ό ν˜ΈμΆœν•˜κ²Œ λœλ‹€
    즉, μ „λ‹¬λœ valueλ₯Ό this.age에 ν• λ‹Ήν•  λ•Œ λ©”λͺ¨λ¦¬μ˜ 값을 μ—…λ°μ΄νŠΈ ν•˜λŠ” 게 μ•„λ‹ˆλΌ setterλ₯Ό ν˜ΈμΆœν•˜κ²Œ λœλ‹€

  3. μœ„ μ˜ˆμ œμ—μ„œλŠ” 1, 2λ₯Ό λ°©μ§€ν•˜κΈ° μœ„ν•΄ getter와 setter μ•ˆμ—μ„œ μ“°μ΄λŠ” λ³€μˆ˜ 이름에 _λ₯Ό μΆ”κ°€ν•΄ λ³€ν™”λ₯Ό μ£Όμ—ˆλ‹€

  4. 결과적으둜 UserλΌλŠ” ν•¨μˆ˜ μ•ˆμ—μ„œ λ³€μˆ˜κ°€ 총 3개(firstName, lastName, _age) 있게 λœλ‹€


4. class 속성(fields)

1) public fields

class μ™ΈλΆ€μ—μ„œλ„ μ ‘κ·Ό κ°€λŠ₯

2) private fields

  • class λ‚΄λΆ€μ—μ„œλ§Œ μ ‘κ·Ό κ°€λŠ₯, μ™ΈλΆ€μ—μ„œλŠ” 값을 읽을 μˆ˜λ„ λ³€κ²½ν•  μˆ˜λ„ μ—†λ‹€

  • classλ₯Ό μ„ μ–Έν•  λ•Œ constructor을 쓰지 μ•Šκ³ , λ°”λ‘œ fieldsλ₯Ό 적을 수 μžˆλ‹€
    μ΄λ•Œ field μ•žμ— #λ₯Ό 뢙이면 private fieldκ°€ λœλ‹€

class Sprite {
  pubilcField = 2;
  #privateField = 0;
}

const coke = new Sprite ();
console.log(coke.pubilcField); // 2
console.log(coke.privateField); // undefined

5. class의 static 속성과 λ©”μ„œλ“œ

  • object의 데이터와 상관없이 classμ—μ„œ κ³΅ν†΅μ μœΌλ‘œ λ°˜λ³΅λ˜μ–΄ μ“°μ΄λŠ” μ†μ„±μ΄λ‚˜ λ©”μ„œλ“œ μ•žμ— static을 λΆ™μ—¬μ€€λ‹€

  • static은 objectλ§ˆλ‹€ ν• λ‹Ήλ˜λŠ” 것이 μ•„λ‹ˆκ³  class μžμ²΄μ— λΆ™μ–΄μžˆλŠ” 것이닀

  • κ·ΈλŸ¬λ―€λ‘œ μƒˆλ‘œ λ§Œλ“€μ–΄μ§„ object의 static μ†μ„±κ°’μ΄λ‚˜ λ©”μ„œλ“œλ₯Ό κ°€μ Έμ˜€κΈ° μœ„ν•΄μ„œλŠ” κ·Έ object의 이름이 μ•„λ‹ˆλΌ class 이름을 μ΄μš©ν•΄μ•Ό ν•œλ‹€

class Article {
  static publisher = 'song';
  construnctor(articleNumber) {
    this.articleNumber = articleNumber;
  }

  static printPublisher() {
    console.log(Article.publisher);
  }
}

const article1 = new Article(1);

console.log(Article.publisher);
// song β†’ console.log(Article1.publisher) ν•˜λ©΄ undefined λ‚˜μ˜΄
Article.printPublisher();
// song β†’ Article1.printPublisher() ν•˜λ©΄ undefined λ‚˜μ˜΄

6. 상속 & λ‹€μ–‘μ„±

1) 상속

extendsλΌλŠ” ν‚€μ›Œλ“œλ₯Ό μ΄μš©ν•˜λ©΄ κ³΅ν†΅λ˜λŠ” 속성과 λ©”μ„œλ“œλ“€μ„ 맀번 일일이 μž‘μ„±ν•˜μ§€ μ•Šκ³ λ„ νŽΈν•˜κ²Œ μž¬μ‚¬μš©ν•  수 μžˆλ‹€.

ex) λ„ν˜• λ§Œλ“€κΈ° (μ‚¬κ°ν˜•, μ‚Όκ°ν˜•)

class Shape {
  constructor (width, height, color) {
    this.width = width;
    this.height = height;
    this.color = color;
  }
 
  draw () {
    console.log(`drawing ${this.color} color of`);
  }
}

// μ‚¬κ°ν˜•
class Rectangle extends Shape {}
// extends ν‚€μ›Œλ“œλ₯Ό μ΄μš©ν•΄ Shape의 속성과 λ©”μ„œλ“œλ“€μ„ Rectangle에 κ·ΈλŒ€λ‘œ κ°€μ Έμ˜΄ (상속)
const rectangle = new Rectangle (20, 20, 'blue');
rectangle.draw(); // drawing blue color of 

// μ‚Όκ°ν˜•
class Triangle extends Shape {}
const triangle = new Triangle (20, 20, 'red');
triangle.draw(); // drawing red color of

2) λ‹€μ–‘μ„±

ν•„μš”ν•œ ν•¨μˆ˜λ§Œ μž¬μ •μ˜(overriding)ν•΄μ„œ μ‚¬μš©ν•  수 μžˆλ‹€.

ex) μ‚¬κ°ν˜• 넓이 β‰  μ‚Όκ°ν˜• 넓이

class Shape {
  constructor (width, height, color) {
    this.width = width;
    this.height = height;
    this.color = color;
  }
 
  draw () {
    console.log(`drawing ${this.color} color of`);
  }

  getArea() {
    return this.width * this.height;
  }
}

class Rectangle extends Shape {}
class Triangle extends Shape {
  draw() {
    super.draw(); // λΆ€λͺ¨μ˜ draw() λ©”μ„œλ“œ 호좜
    console.log('❀'); // μž¬μ •μ˜(overriding)
  }
  getArea() {
    return (this.width * this.height) / 2; // μž¬μ •μ˜(overriding)
  }
}

// μ‚¬κ°ν˜•
const rectangle = new Rectangle (20, 20, 'blue');
rectangle.draw(); // drawing blue color of
console.log(rectangle.getArea()); // 400

// μ‚Όκ°ν˜•
const triangle = new Triangle (20, 20, 'red');
triangle.draw(); // drawing red color of ❀
console.log(triangle.getArea()); // 200

ν•œνŽΈ,
βœ” draw() λ©”μ†Œλ“œλŠ” rectangle.draw();라고 쳐도 값이 λ‚˜μ˜¨λ‹€.
βœ” getArea() λ©”μ†Œλ“œλŠ” console.log(triangle.getArea());라고 쳐야 값이 λ‚˜μ˜¨λ‹€.


7. instanceof μ—°μ‚°μž

  • instanceOf μ™Όμͺ½μ— μžˆλŠ” objectκ°€ 였λ₯Έμͺ½μ— μžˆλŠ” classλ₯Ό μ΄μš©ν•΄ λ§Œλ“€μ–΄μ§„ object, 즉 instance인지 μ•„λ‹Œμ§€λ₯Ό ν™•μΈν•΄μ£ΌλŠ” μ—°μ‚°μž

  • true ν˜Ήμ€ false둜 returnν•œλ‹€

console.log(triangle instanceof Triangle); // true
console.log(triangle instanceof Shape); // true
console.log(triangle instanceof Object); // true

πŸ’‘ 3번째 쀄 μ„€λͺ…

  • μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œ λ§Œλ“  λͺ¨λ“  object/class듀은 μžλ°”μŠ€ν¬λ¦½νŠΈμ— μžˆλŠ” objectλ₯Ό μƒμ†ν•œ 것이닀.
  • λ”°λΌμ„œ μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œ λ§Œλ“  λͺ¨λ“  object듀은 μžλ°”μŠ€ν¬λ¦½νŠΈμ— μžˆλŠ” Object의 λ©”μ„œλ“œλ“€μ„ κ°€μ Έμ™€μ„œ μž¬μ •μ˜ν•  수 μžˆλ‹€.
    (VS codeμ—μ„œ object μœ„μ— ctrl/command λˆ„λ₯΄κ³  ν΄λ¦­ν•˜λ©΄ λ©”μ„œλ“œ 확인 κ°€λŠ₯)

πŸ™‹β€β™€οΈ 질문

  1. μ—¬κΈ° λ°”λ‘œκ°€κΈ° (μ ‘κ·Όμž ν”„λ‘œνΌν‹° getter / setter) β†’ μ΄ν•΄ν–ˆλ‹€ [TIL] 211010 μ°Έκ³ 

✨ 내일 ν•  것

  1. λ…Έλ§ˆλ“œ 코더 유튜브 '바닐라 μžλ°”μŠ€ν¬λ¦½νŠΈ' κ°•μ˜ 1-10(object) μˆ˜κ°•

  2. λ“œλ¦Όμ½”λ”© 유튜브 'μžλ°”μŠ€ν¬λ¦½νŠΈ' κ°•μ˜ 7편(object) μˆ˜κ°•

profile
λŠ₯λ™μ μœΌλ‘œ μ‚΄μž, ν–‰λ³΅ν•˜κ²ŒπŸ˜

0개의 λŒ“κΈ€