TIL - 21.07.19 ๐Ÿ‘จโ€๐Ÿ’ป - Js, OOP

TIL - 21.07.19 ๐Ÿ‘จโ€๐Ÿ’ป

  • Set ๊ฐ์ฒด
  • ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ

Set ๊ฐ์ฒด

Set ๊ฐ์ฒด๋Š” ๋ฐฐ์—ด๊ณผ ์œ ์‚ฌํ•œ ๊ฐ์ฒด๋กœ ํ”„๋กœํผํ‹ฐ ํ‚ค๋กœ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ์ˆซ์ž๋ฅผ ๊ฐ€์ง€๋ฉฐ, ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ ๊ฐ’์œผ๋กœ ์œ ์ผํ•œ ๊ฐ’๋“ค๋งŒ ๊ฐ€์ง„๋‹ค๋Š” ํŠน์ง•์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ( ๋ฌผ๋ก  ์ฐธ์กฐ ์ž๋ฃŒํ˜•์˜ ํŠน์„ฑ์— ์˜ˆ์™ธ๋Š” ์—†๋‹ค. )
์›๋ž˜ NaN !== NaN ์ด์ง€๋งŒ, Set ๊ฐ์ฒด ์•ˆ์—์„œ๋Š” ์œ ์ผํ•œ ๊ฐ’์œผ๋กœ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋กœ ํ•˜๋‚˜์˜ NaN๋งŒ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.

์—ฌ๋Ÿฌ ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ๋Š”๋ฐ ๋Œ€ํ‘œ์ ์ธ ๋ฉ”์†Œ๋“œ ์„ธ ๊ฐœ๋ฅผ ์†Œ๊ฐœํ•˜์ž๋ฉด,
.add() - Set ๊ฐ์ฒด์— ํ”„๋กœํผํ‹ฐ ์ถ”๊ฐ€ํ•œ๋‹ค.
.has() - Set ๊ฐ์ฒด์— ๊ด„ํ˜ธ ์•ˆ์˜ ์ธ์ž๊ฐ€ ์žˆ๋Š”์ง€ ๋ถˆ๋ฆฌ์–ธ ํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
.size - Set ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ ์ˆ˜๋ฅผ ์ˆซ์ž๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

let arr = [1, 2, 3, 3, 4]
let set = new Set([...arr]) // set { 1, 2, 3, 4 }; ์ค‘๋ณต๋˜๋Š” ํ”„๋กœํผํ‹ฐ 3 ํ•˜๋‚˜ ์ œ๊ฑฐ.
set.add(5) // set { 1, 2, 3, 4, 5 }
set.add(3) // set { 1, 2, 3, 4, 5 }; 3์€ ์ด๋ฏธ ์žˆ์œผ๋ฏ€๋กœ ์ถ”๊ฐ€๋˜์ง€ ์•Š๋Š”๋‹ค.
set.add([1, 2]) // set { 1, 2, 3, 4, 5, [1, 2] } 
set.add([1, 2]) // set { 1, 2, 3, 4, 5, [1, 2], [1, 2] }; ์ฐธ์กฐ ์ž๋ฃŒํ˜• ์ด๊ธฐ์—
set.has(2) // true;
set.has([1, 2]) // false; ์—ญ์‹œ ์ฐธ์กฐ ์ž๋ฃŒํ˜• ์ด๊ธฐ์—.
set.size // 7; ์ˆซ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ( Object Orient Programming )

๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ ์ ˆ์ฐจ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ๊ณผ ๋‹ค๋ฅด๊ฒŒ ๋ฐ์ดํ„ฐ์™€ ๊ธฐ๋Šฅ์„ ๋ฌถ์–ด์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค.
์†์„ฑ๊ณผ ๋ฉ”์†Œ๋“œ๊ฐ€ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋ผ๋Š” ๊ฐœ์—ด์— ์กฐํ•ฉ๋˜๋ฉฐ ์ด๋ฅผ class๋ผ ํ•œ๋‹ค.


์ฃผ์˜ํ•  ์ 

  • ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ์‹œ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.
  • ํด๋กœ์ €์™€ ๋น„์Šทํ•œ ๋ฐฉ์‹์œผ๋กœ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ  ๊ทธ ํ•จ์ˆ˜๋ฅผ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๋ฉด ๋ณ€์ˆ˜๋ฅผ ๋ณ„๊ฐœ์˜ ๋…๋ฆฝ๋œ ๊ฐ์ฒด๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
// ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด ๋งŒ๋“  ์นด์šดํ„ฐ
const counter = {
    value: 0,
    increase: function(){
    this.value++
    },
    decrease: function(){
    this.value--
    },
    getValue: function(){
    return this.value
    }
}

// ํด๋กœ์ €๋ฅผ ์ด์šฉํ•ด ๋งŒ๋“  ์นด์šดํ„ฐ
const makeCounter = () => {
  return {
    value: 0,
    increase: function(){
    this.value++
    },
    decrease: function(){
    this.value--
    },
    getValue: function(){
    return this.value
    }
  }
}

let counter1 = makeCounter();
counter1.increase();
counter1.increase();
counter1.increase();
counter1.getValue(); // 3 

let counter2 = makeCounter();
counter2.decrease();
counter2.decrease();
counter2.decrease();
counter2.getValue(); // -3; ํด๋กœ์ €์˜ ํŠน์„ฑ์„ ๋”ฐ๋ผ ๊ฐ๊ฐ ๋‹ค๋ฅธ value๋ฅผ ๊ฐ€์ง„๋‹ค.

class ์™€ instance

์˜ˆ๋ฅผ ๋“ค์–ด, ์–ด๋–ค ๋ฌผ๊ฑด์„ ๋งŒ๋“ค๋•Œ ํ•˜๋‚˜์˜ ์ฒญ์‚ฌ์ง„(blue print)์„ ๋งŒ๋“ค๊ณ  ๊ทธ ์ฒญ์‚ฌ์ง„์— ๋”ฐ๋ผ ๋ฌผ๊ฑด์„ ๋งŒ๋“ค๋ฉด ๊ฐ๊ฐ์˜ ๋…๋ฆฝ๋œ ๋ฌผ๊ฑด์ด ๋งŒ๋“ค์–ด์ง„๋‹ค.
์—ฌ๊ธฐ์„œ ์ฒญ์‚ฌ์ง„์„ class๋ผ๊ณ  ํ•˜๊ณ , ๋ฌผ๊ฑด์„ instance Object, ์ค„์—ฌ์„œ instance๋ผ๊ณ  ํ•œ๋‹ค.

  • ์—ฌ๊ธฐ์„œ instance๋ฅผ ๋งŒ๋“ค๋•Œ, ์ฆ‰ ๋ณ€์ˆ˜์— class๋ฅผ ํ• ๋‹นํ• ๋•Œ, ์ผ๋ฐ˜ ํ•จ์ˆ˜ ํ• ๋‹นํ•˜๋“ฏ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ newํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ํ•จ์ˆ˜์˜ ์ฒซ ๊ธ€์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ์ž‘์„ฑํ•œ๋‹ค.

  • ES5๊นŒ์ง€ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๋• ์ผ๋ฐ˜ ํ•จ์ˆ˜ ๋งŒ๋“ค๋“ฏ function์„ ์‚ฌ์šฉํ•˜๋˜, ํ•จ์ˆ˜ ์ด๋ฆ„์˜ ์ฒซ ๊ธ€์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ํ‘œ๊ธฐํ•ด ๊ตฌ๋ถ„ํ–ˆ๋‹ค๋ฉด, ๋น„๊ต์  ์ตœ์‹ ์ธ ES6๋ฌธ๋ฒ•์—์„  class๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

  • ์ด class ๋‚ด instance๊ฐ€ ๋งŒ๋“ค์–ด์งˆ๋•Œ ์‹คํ–‰๋  ์ƒ์„ฑ์ž ํ•จ์ˆ˜(constructor)๋ฅผ ๋งŒ๋“ ๋‹ค.
    ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” ๋ฐ˜ํ™˜ ๊ฐ’์„ ๊ฐ€์ง€์ง€ ์•Š์œผ๋ฉฐ ๊ฐ์ฒด์— ํ• ๋‹นํ•  ํ”„๋กœํผํ‹ฐ ์ •๋ณด๊ฐ€ ๋“ค์–ด์žˆ์ง€๋งŒ, ๊ฐ์ฒด๊ฐ€ ์•„๋‹ˆ๋ผ ํ•จ์ˆ˜์ด๊ธฐ์— ์ฝค๋งˆ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

  • ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ this๋Š” instance ๊ฐ์ฒด๋ฅผ ๋œปํ•œ๋‹ค.

  • ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ํ˜ธ์ถœํ• ๋• instance.key; ์™€ ๊ฐ™์ด ํ˜ธ์ถœํ•˜๋ฉด ๋˜์ง€๋งŒ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ• ๋• ๋‹ค๋ฅธ ๋‚ด์žฅ ๋ฉ”์†Œ๋“œ์™€ ๊ฐ™์ด instance.method(); ์™€ ๊ฐ™์ด ์†Œ๊ด„ํ˜ธ๋ฅผ ์ ์–ด์ค€๋‹ค.

// ES5
function Developer(name, gender, age){
  // ์ƒ์„ฑ์ž ํ•จ์ˆ˜
  this.name = name;
  this.gender = gender;
  this.age = age;
}

// ๋ฉ”์†Œ๋“œ ์ƒ์„ฑ, className.prototype ์— ๋ฉ”์†Œ๋“œ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
Developer.prototype.coding = function(){
  console.log(this.name + '์ด/๊ฐ€ ์ฝ”๋”ฉ์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค!')
}

//ES6
class Developer(){
  // ์ƒ์„ฑ์ž ํ•จ์ˆ˜
  constructor(name, gender, age){
    this.name = name;
    this.gender = gender;
    this.age = age;
  }
  // ๋ฉ”์†Œ๋“œ ์ƒ์„ฑ
  coding(){
    console.log(this.name + '์ด/๊ฐ€ ์ฝ”๋”ฉ์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค!')
  }

  let newDev = new Developer('sunghoon', 'male', '26') 
  // ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ๋ฒ• ์ฐจ์ด๋Š” ์—†๋‹ค.

  newDev.name // 'sunghoon';
  newDev.coding() // 'sunghoon์ด/๊ฐ€ ์ฝ”๋”ฉ์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค!'

์ค‘๊ฐ„ ์šฉ์–ด ์ •๋ฆฌ

  • prototype - ๋ชจ๋ธ์˜ ์ฒญ์‚ฌ์ง„์„ ๋งŒ๋“ค๋•Œ ์“ฐ๋Š” ์›ํ˜• ๊ฐ์ฒด(original form)
  • constructor - instance๊ฐ€ ์ดˆ๊ธฐํ™”๋ ๋•Œ ์‹คํ–‰ํ•˜๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜
  • this - ํ•จ์ˆ˜ ์‹คํ–‰์‹œ ํ•ด๋‹น scope ๋งˆ๋‹ค ์ƒ์„ฑ๋˜๋Š” ๊ณ ์œ ํ•œ ์‹คํ–‰(execution) context.
    newํ‚ค์›Œ๋“œ๋กœ instance ์ƒ์„ฑ์‹œ ํ•ด๋‹น instance๊ฐ€ this ๊ฐ’์ด ๋จ.

๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ 4๊ฐ€์ง€ ํŠน์ง•

  • ์บก์Šํ™” ( Encapsulation )
    ์ฝ”๋“œ๊ฐ€ ๋ณต์žกํ•˜์ง€ ์•Š๊ฒŒํ•˜๊ณ  ์žฌ ์‚ฌ์šฉ์„ฑ์„ ๋†’ํž˜

  • ์ถ”์ƒํ™” ( Abtraction )
    ์—ญ์‹œ ์ฝ”๋“œ๋ฅผ ๋ณต์žกํ•˜์ง€ ์•Š๊ฒŒํ•˜๊ณ , ๋‹จ์ˆœํ™”๋œ ์‚ฌ์šฉ์œผ๋กœ ๋ณ€ํ™”์˜ ์˜ํ–ฅ์„ ์ตœ์†Œํ™”ํ•จ.

  • ์ƒ์† ( Inheritance )
    ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’ํž˜.

  • ๋‹คํ˜•์„ฑ ( Polymorphism )
    ๋™์ผํ•œ ๋ฉ”์†Œ๋“œ์— ๋Œ€ํ•ด if/else if ์™€ ๊ฐ™์€ ์กฐ๊ฑด๋ฌธ ๋Œ€์‹  ๊ฐ์ฒด์˜ ํŠน์„ฑ์— ๋งž๊ฒŒ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ด์ง.


์บก์Šํ™”

์บก์Šํ™”์˜ ์š”์ ์€ ๋ฐ์ดํ„ฐ(ํ”„๋กœํผํ‹ฐ)์™€ ๊ธฐ๋Šฅ(๋ฉ”์†Œ๋“œ)์„ ํ•˜๋‚˜์˜ ๋‹จ์œ„๋กœ ๋ฌถ๋Š” ๊ฒƒ์ด๋ผ๋Š” ๊ฒƒ์ด๋‹ค.
์ด ๋ง์€ ๋ฐ์ดํ„ฐ์™€ ๊ธฐ๋Šฅ์ด ๋Š์Šจํ•˜๊ฒŒ ๊ฒฐํ•ฉ๋˜๋Š” ๊ฒƒ์„ ๋œปํ•œ๋‹ค.

  • ๋Š์Šจํ•œ ๊ฒฐํ•ฉ์ด๋ž€?
    ์ฝ”๋“œ ์‹คํ–‰ ์ˆœ์„œ์— ๋”ฐ๋ฅธ ์ ˆ์ฐจ์  ์ฝ”๋“œ ์ž‘์„ฑ์ด ์•„๋‹Œ ์ฝ”๋“œ๊ฐ€ ์ƒ์ง•ํ•˜๋Š” ์‹ค์ œ ๋ชจ์Šต๊ณผ ๋‹ฎ๊ฒŒ ์ฝ”๋“œ๋ฅผ ๋ชจ์•„ ๊ฒฐํ•ฉํ•˜๋Š” ๊ฒƒ

์บก์Šํ™” ํŠน์ง•์œผ๋กœ ํŒŒ์ƒ๋˜๋Š” ๊ธฐ๋Šฅ์ด ํ•œ๊ฐ€์ง€ ๋” ์žˆ๋Š”๋ฐ, ๋ฐ”๋กœ ์€๋‹‰ํ™”์ด๋‹ค.

์€๋‹‰ํ™”๋Š” ๋‚ด๋ถ€ ๋ฐ์ดํ„ฐ๋‚˜ ๋‚ด๋ถ€ ๊ตฌํ˜„์ด ์™ธ๋ถ€๋กœ ๋…ธ์ถœ๋˜์ง€ ์•Š๋„๋ก ๋งŒ๋“œ๋Š” ๊ฒƒ์„ ๋œปํ•˜๊ณ , ์‰ฝ๊ฒŒ ๋งํ•ด ๊ตฌํ˜„์€ ์ˆจ๊ธฐ๊ณ , ๋™์ž‘์€ ๋…ธ์ถœ์‹œํ‚ค๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.

์€๋‹‰ํ™”๋กœ ๋””ํ…Œ์ผํ•œ ๊ตฌํ˜„์ด๋‚˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆจ๊ธฐ๊ณ  ๊ฐ์ฒด ์™ธ๋ถ€์—์„œ ํ•„์š”ํ•œ ๋™์ž‘๋งŒ ๋…ธ์ถœ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

๊ณ ๋กœ ์–ธ์ œ๋“  ๊ตฌํ˜„์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋Š์Šจํ•œ ๊ฒฐํ•ฉ์— ์œ ๋ฆฌํ•˜๋‹ค.

--

์ถ”์ƒํ™”

์ถ”์ƒํ™”๋Š” ๋‚ด๋ถ€ ๊ตฌํ˜„์€ ์•„์ฃผ ๋ณต์žกํ•˜์ง€๋งŒ ๋…ธ์ถœ๋˜๋Š” ๋ถ€๋ถ„์€ ๋‹จ์ˆœํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค๋Š” ๊ฐœ๋…์ด๋‹ค.
ํด๋ž˜์Šค ์ •์˜์‹œ ๋ฉ”์†Œ๋“œ์™€ ์†์„ฑ๋งŒ ์ •์˜ํ•œ ๊ฒƒ์„ ์ธํ„ฐํŽ˜์ด์Šค๋ผ๊ณ ํ•˜๊ณ , ์ด ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๊ณง ์ถ”์ƒํ™”์˜ ๋ณธ์งˆ์ด๋‹ค.


์บก์Šํ™” vs ์ถ”์ƒํ™”

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


์ƒ์†

์ƒ์†์€ ๊ธฐ๋ณธ ํด๋ž˜์Šค(base class)์˜ ํŠน์ง•์„ ํŒŒ์ƒ ํด๋ž˜์Šค(derive class)๊ฐ€ ๋ฌผ๋ ค ๋ฐ›๋Š” ๊ฒƒ์„ ๋œปํ•œ๋‹ค.

์ƒ์†์„ ๋ฐ›์„๋• ํด๋ž˜์Šค ์ด๋ฆ„ ๋’ค์— extends ํ‚ค์›Œ๋“œ๋ฅผ ๋„ฃ์–ด์„œ ์ƒ์† ๋ฐ›์„ ํด๋ž˜์Šค๋ฅผ ๋ช…๊ธฐํ•ด์ค€๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์— super ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์ƒ์† ๋ฐ›์„ ํด๋ž˜์Šค์—์„œ ์ƒ์†๋ฐ›์„ ์†์„ฑ์„ ๋ช…๊ธฐํ•œ๋‹ค.

super ํ‚ค์›Œ๋“œ๋Š” ์ธ์ž๊ฐ€ ์žˆ๋“  ์—†๋“  ์ƒ๊ด€์—†์ด base class์—์„œ ๋ชจ๋“  ์†์„ฑ์„ ์ƒ์† ๋ฐ›๋Š”๋‹ค.
๋‹ค๋งŒ, ํŒŒ์ƒ ํด๋ž˜์Šค์—์„œ ์ƒ์† ๋ฐ›์€ ํ”„๋กœํผํ‹ฐ๋ฅผ ์žฌํ• ๋‹นํ•˜๊ณ  ์‹ถ์„๋•Œ๋Š” super์˜ ์ธ์ž์— ๋ช…๊ธฐํ•ด์ค˜์•ผ ํ•œ๋‹ค.

๋ฉ”์†Œ๋“œ์˜ ๊ฒฝ์šฐ๋Š” superํ‚ค์›Œ๋“œ์—์„œ ๋”ฐ๋กœ ๋ช…๊ธฐํ•˜์ง€ ์•Š๋”๋ผ๋„ extends ํ•ด์˜ฌ๋•Œ ๋ฐ›์•„์˜จ๋‹ค.

class Human {
  constructor(name, age){
  	this.name = name;
    this.age = age;
    this.gender = 'male';
  }
}

class Student extends Human {
  constructor(name, age, gender, grade){
    super();
    this.grade = grade;
  }
}
 
let student1 = new Student('sunghoon', 10, 'female', 'A');
console.log(student1) 
//  Student {name: undefined, age: undefined, gender: "male", grade: "A"}
// ์ƒ์†๋ฐ›์€ ๊ฐ’ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ–ˆ๊ธฐ์— student1์˜ gender ๊ฐ’์— female์„ ๋„ฃ์–ด๋„ 
// male์„ ๊ฐ’์œผ๋กœ ๊ฐ€์ง„ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋๋‹ค.

class Student extends Human {
  constructor(name, age, gender, grade){
    super(name, age, gender);
    this.grade = grade;
  }
}
let student1 = new Student('sunghoon', 10, 'female', 'A');
console.log(student1) 
// Student {name: "sunghoon", age: 10, gender: "male", grade: "A"}
// ์ด๋ฒˆ์—” ์ œ๋Œ€๋กœ ์ ์šฉ๋˜์—ˆ์œผ๋‚˜ gender์˜ ๊ฒฝ์šฐ ์• ์ดˆ์— base class์—์„œ ๊ฐ’์„ ๊ณ ์ •์„ ์‹œ์ผœ๋†“๊ณ  
// ๋งค๊ฐœ๋ณ€์ˆ˜ ์ž๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด ๋†“์ง€ ์•Š์•˜๊ธฐ์— derive class์—์„œ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์— gender์˜ 
// ๋งค๊ฐœ๋ณ€์ˆ˜ ์ž๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด๋„ ์ ์šฉ์ด ์•ˆ๋˜์—ˆ๋‹ค.

class Student extends Human {
  constructor(name, age, gender, grade){
    super(name, age);
    this.grade = grade;
    this.gender = gender;
  }
}
let student1 = new Student('sunghoon', 10, 'female', 'A');
console.log(student1)
// Student {name: "sunghoon", age: 10, gender: "female", grade: "A"}
// this๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ๊ฒƒ์œผ๋กœ ๋“œ๋””์–ด ์„ฑํ›ˆ์ด๊ฐ€ ์—ฌ์ž๊ฐ€ ๋˜์—ˆ๋‹ค.

๋‹คํ˜•์„ฑ

๋‹คํ˜•์„ฑ์€ ๊ฐ™์€ ๋ฉ”์†Œ๋“œ๋ผ๋„ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌํ˜„๋  ์ˆ˜ ์žˆ์Œ์„ ๋œปํ•œ๋‹ค.


prototypes ํ˜ธ์ถœ ๋ชจํ˜•


์ด๊ฑด ๋จธ๋ฆฌ๊ฐ€ ๋ณต์žกํ•ด์„œ ์•„์ง ์ •๋ฆฌํ•  ์ค€๋น„๊ฐ€ ์•ˆ๋๋‹ค.


private, static ๊ฒ‰ํ•ก๊ธฐ

private ํ‚ค์›Œ๋“œ๋Š” ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ๋งŒ ์“ฐ์ด๋Š” ์†์„ฑ, ๋ฉ”์†Œ๋“œ ๊ตฌ๋ถ„๋ฒ•๋‹ˆ๋‹ค.
์ง€์›ํ•˜๋Š” ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ ์–ด JavaScript ์—์„œ๋Š” ํด๋กœ์ €๋กœ ์€๋‹‰ํ™”๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.

static ํ‚ค์›Œ๋“œ๋Š” ํด๋ž˜์Šค์˜ ์ •์  ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•œ๋‹ค.


GOOD ๐Ÿ˜‰

๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์— ๋Œ€ํ•œ ๊ฐœ๊ด„์ ์ธ ์ดํ•ด๊ฐ€ ์ƒ๊ฒผ๋‹ค.

BAD ๐Ÿ˜ฅ

์นœํ•ด์ง€๋Š”๋ฐ ์‹œ๊ฐ„์ด ์ข€ ๊ฑธ๋ฆด ๋“ฏ ํ•˜๋‹ค.

TO DO ๐Ÿ”ฅ

  • ํ”„๋กœํ† ํƒ€์ž… ๊ฐœ๋… ๋ช…ํ™•ํžˆ
  • ์žฌ๊ท€ํ•จ์ˆ˜
  • ๋…ธ๋งˆ๋“œ์ฝ”๋” ๋ฆฌ์•กํŠธ ๋งˆ๋ฌด๋ฆฌ

Retrospect ๐Ÿง

์˜ค๋Š˜์€ ์„น์…˜ 2 ์ฒซ๋‚ ์ด์—ˆ๋‹ค.
๋ญ”๊ฐ€ ์ฒซ๋‚ ์ด๋ผ์„œ ์•ฝํ•˜๊ฒŒ ๋•Œ๋ฆฐ๊ฑด๊ฐ€ ์•„์ง๊นŒ์ง„ ํ• ๋งŒํ•œ ๋Š๋‚Œ์ด ๋“ ๋‹ค.
ํ”„๋กœํ† ํƒ€์ž…๋งŒ ์–ด๋–ป๊ฒŒ ์ž˜ ์ดํ•ดํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™์€๋ฐ, ์ด๊ฒŒ ๋Œ€๋žต์ ์œผ๋กœ ์–ด๋–ป๊ฒŒํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ํ˜ธ์ถœ๋˜๊ณ  ์ด๋Ÿฐ ๊ฒƒ ๊นŒ์ง„ ํ• ๋งŒํ•œ๋ฐ ๋ช…ํ™•ํ•˜๊ฒŒ ๊ฐœ๋…์ด ์ •๋ฆฝ์ด ๋˜์–ด์žˆ์ง€ ์•Š์€ ๋Š๋‚Œ์ด ๋“ ๋‹ค.

์˜ค๋Š˜ ์ปคํ”ผ๊ฐ€ ๋งˆ์นจ ๋‹ค ๋–จ์–ด์ ธ์„œ ์นดํŽ˜์ธ ์—†์ด ๊ณต๋ถ€ํ•˜๋‹ค๋ณด๋‹ˆ ์ง‘์ค‘๋ ฅ๋„ ๋–จ์–ด์ง€๊ณ  ํ”ผ๊ณคํ•จ์ด ๋ฐฐ๋กœ ๋Š˜์–ด๋‚œ ๋Š๋‚Œ์ด๋‹ค.

๋นจ๋ฆฌ์ž๊ณ  ๋‚ด์ผ ๋นจ๋ฆฌ ์ผ์–ด๋‚˜์„œ ์žฌ๊ท€ํ•จ์ˆ˜์ข€ ๋ฏธ๋ฆฌ ์กฐ์ ธ๋†”์•ผ๊ฒ ๋‹ค.
๋‚ด๊ฐ€ ์žฌ๊ท€ํ•จ์ˆ˜์—๊ฒŒ ์กฐ์ง๋‹นํ•  ์˜ˆ์ •

Reference ๐Ÿ™‡

https://developer.mozilla.org/

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