Logo
Hyunsu Blog
inheritance

๐Ÿ“†Published :May 29, 2021 โ€ข

๐Ÿ“†Updated :May 29, 2021 โ€ข

โ˜•๏ธ5min

OOP Inheritance in JavaScript

  • ์ด ํฌ์ŠคํŠธ๋Š” ์นด์ผ์‹ฌ์Šจ์˜ YOU DON'T KNOW JS, ์ •์ง„์šฑ๋‹˜์˜ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ํ€ต์Šคํƒ€ํŠธ๋ฅผ ์ฐธ๊ณ  ํ•˜์—ฌ ์ดํ•ดํ•œ ๋ฐ”ํƒ•์œผ๋กœ ์ ์€ ๋…ธํŠธ์ž…๋‹ˆ๋‹ค. ๋ฐœ์ „ํ•˜๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ๋  ์ˆ˜ ์žˆ๋„๋ก ์„ค๋ช…์ด ์ž˜๋ชป ๋˜์—ˆ๋‹ค๊ฑฐ๋‚˜ ์กฐ๊ธˆ ๋” ๋ณด์ถฉ์„ค๋ช…์ด ํ•„์š” ํ•˜์‹œ๋‹ค๊ณ  ์ƒ๊ฐ๋˜์‹œ๋ฉด ๋Œ“๊ธ€ ๋‚จ๊ฒจ ์ฃผ์„ธ์š”. ์–ธ์ œ๋“ ์ง€ ํ™˜์˜์ž…๋‹ˆ๋‹ค ๐Ÿ˜„

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ๋งํ•˜๋Š” ์ƒ์† (ES6,ES5)

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์“ฐ๋ฉด ์“ธ์ˆ˜๋ก ์™œ ๊ทธ๋ ‡์ง€ ? ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋Š” ๋ถ€๋ถ„์˜ ๋‹ต์—๋Š” prototype์ด ์ž๋ฆฌ๋ฅผ ์ง€ํ‚ค๊ณ  ์„œ ์žˆ์—ˆ๋‹ค. ์ด ๊ธ€์—์„œ ๊ทธ๋Ÿฐ ๋ถ€๋ถ„์„ ๋‚˜ ์Šค์Šค๋กœ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ฝ๋Š” ๋ถ„๋“ค์—๊ฒŒ๋„ ๋„์›€์ด ๋˜์—ˆ์œผ๋ฉด ํ•œ๋‹ค.

์ผ๋‹จ OOP ์ƒ์† ๊ด€๊ณ„์— ๋Œ€ํ•œ theme์„ ๋งํ•˜์ž๋ฉด, ์ƒ์†์€ ์ƒ์œ„/๋ถ€๋ชจ ํด๋ž˜์Šค์— ์†ํ•ด ์žˆ๋Š” ๋ฉ”์„œ๋“œ์™€ ์†์„ฑ ๋“ค์„ ๋ฌผ๋ ค ๋ฐ›๋Š” ์ƒˆ๋กœ์šด child ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์ˆ˜๊ฐ€ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ ๋งํ•˜๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค๋Š” superclass/base class๋กœ ๋ถˆ๋ ค์ง€๊ธฐ๋„ ํ•˜๊ณ  childํด๋ž˜์Šค๋Š” subclasses/derived classes๋กœ ๋ถˆ๋ ค์ง€๊ธฐ๋„ ํ•œ๋‹ค. ์ƒ์†๊ด€๊ณ„๋Š” ํด๋ž˜์Šค๊ฐ„์˜ 'kind-of' or 'is-a' ๊ด€๊ณ„๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด "a dog is an animal" or "a dog is a kind of animal". ๊ฐœ๋Š” ๋™๋ฌผ์ด๋‹ค, ๊ฐœ๋Š” ๋™๋ฌผ์˜ ํ•œ ์ข…๋ฅ˜์ด๋‹ค. ๋ผ๋Š” ๊ด€๊ณ„๊ฐ€ ์„ฑ๋ฆฝํ•œ๋‹ค. 'HAS-A' ๋ผ๋Š” ํฌํ•จ๊ด€๊ณ„๋„ ์žˆ๋Š”๋ฐ ํ•œ ํด๋ž˜์Šค์—์„œ ๋‹ค๋ฅธ ํด๋ž˜์Šค๋ฅผ ๋ฉค๋ฒ„๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•˜๋Š” ๊ฒƒ์ด๋‹ค. oop ๊ธฐ์ˆ ์ธ ์ƒ์†์€ ์ž์‹ํด๋ž˜์Šค๊ฐ€ ๋ถ€๋ชจํด๋ž˜์Šค์—์„œ ํŒŒ์ƒ๋˜๊ณ  ๋ถ€๋ชจ์˜ ๋ชจ๋“  ์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ๋ฅผ ์ด์–ด๋ฐ›๋Š”๋‹ค. ์ด๋ ‡๊ฒŒ ์ด์–ด๋ฐ›์€ ์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ๋Š” ์ž์‹ํด๋ž˜์Šค์—์„œ ๋‹ค์‹œ ์žฌ์ •์˜ ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ํ•˜์ง€๋งŒ ํ•„์š”์— ๋”ฐ๋ผ ์ž์‹ํด๋ž˜์Šค์—์„œ ๋ฉ”์„œ๋“œ๋‚˜ ์†์„ฑ์„ ์ถ”๊ฐ€ํ•˜๊ธฐ๋„ ํ•˜๊ณ , ์ƒ์†๋œ ๋ฉ”์„œ๋“œ์˜ ๋‚ด๋ถ€ ๋กœ์ง์„ ๋ณ€๊ฒฝํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ES06 ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋„ class, extends๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋Ÿฐ ์ƒ์†์ ์ธ ๊ฐœ๋…์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

1 . IS-A

class Bicycle { constructor() { this.numberOfWheel = 4 //<-privateํ•˜๊ฒŒ ํ•˜๊ณ  ์ฐจ์ผ๋“œ๊ฐ€ ๊ฐ€์ง€๊ณ  ์˜ฌ ์ˆœ ์—†๋‚˜? } } class MountainBike extends Bicycle { constructor() { super(numberOfWheel) } getNumOfWheel() { return this.numOfWheel } }

2 . HAS-A

  • ํ•ฉ์„ฑ๊ด€๊ณ„ (composition)-๊ฐ•ํ•œ๊ด€๊ณ„
    engine ๊ฐ์ฒด๋Š” ์ธ์Šคํ„ด์Šคํ™” ๋  ๋•Œ ์ƒ์„ฑ๋˜๊ณ  myCarr๊ฐ€ null์ด ๋˜๋ฉด engine ๊ฐ์ฒด๋„ ์ œ๊ฑฐ๋œ๋‹ค.
class Engine {} class Car { #engine constructor() { this.#engine = new Engine() } } let myCar = new Car() myCar = null
  • ์ง‘ํ•ฉ๊ด€๊ณ„ (aggregation)- ์•ฝํ•œ๊ด€๊ณ„ car๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ์™ธ๋ถ€์—์„œ engine ๊ฐ์ฒด๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌํ•œ๋‹ค. car ๊ฐ์ฒด๊ฐ€ null ์ด ๋˜๋”๋ผ๋„ engine ๊ฐ์ฒด๋Š” ์‚ฌ๋ผ์ง€์ง€ ์•Š๋Š”๋‹ค.
class Engine {} class car { #engine constructor(engine) { this.engine = engine } } let engine = new Engine() let car = new Car(engine)

ES06 ์ƒ์† ๊ตฌํ˜„ ํ•˜๊ธฐ

ES06 ํด๋ž˜์Šค์—์„  super์™€ this ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•ด ๋ถ€๋ชจํด๋ž˜์Šค์˜ ์„ ์–ธ๋œ ๋ฉค๋ฒ„ ๋ฉ”์„œ๋“œ๋‚˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • super - ๋ถ€๋ชจํด๋ž˜์Šค์˜ public ๋ฉค๋ฒ„๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅ.
  • this - ์ƒ์†๋ฐ›์€ ๋ฉค๋ฒ„์™€ ํ˜„์žฌํด๋ž˜์Šค๋‚ด์˜ ๋ชจ๋“  ๋ฉค๋ฒ„ ์ ‘๊ทผ๊ฐ€๋Šฅ.
class PC { #ram constructor(hddCapacity) { this.hddCapacity = hddCapacity this.#ram = '0GB' } set ramCapacity(value) { this.#ram = value } get ramCapacity() { return this.#ram } getHddCapacity() { return this.hddCapacity } } class Desktop extends PC { ํผ constructor(hddCapacity) { super(hddCapacity) } getInfo() { console.log( `1. HDD capacity : ${super.getHddCapacity()},${super.hddCapacity}`, ) console.log( `2. HDD capacity : ${this.getHddCapacity()},${this.hddCapacity}`, ) this.hddCapacity = '2000G' console.log( `3. HDD capacity : ${super.getHddCapacity()},${super.hddCapacity}`, ) console.log( `4. HDD capacity : ${this.getHddCapacity()},${this.hddCapacity}`, ) super.ramCapacity = '16G' console.log(`5. RAM capacity : ${this.ramCapacity},${super.ramCapacity}`) this.ramCapacity = '8G' console.log(`6. RAM capacity : ${this.ramCapacity},${super.ramCapacity}`) } } let mydesktop = new Desktop('500G') mydesktop.getInfo() // 1. HDD capacity : 500G,undefined // 2. HDD capacity : 500G,500G // 3. HDD capacity : 2000G,undefined // 4. HDD capacity : 2000G,2000G // 5. RAM capacity : 16G,16G // 6. RAM capacity : 8G,8G

ํ™•์žฅ๋œ ํด๋ž˜์Šค(Desktop)์—์„œ ์ƒ์œ„ ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„๋ณ€์ˆ˜์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด์„ , ๋ถ€๋ชจ ํด๋ž˜์Šค ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•ด์ค˜์•ผํ•œ๋‹ค. ๋ฐฉ๋ฒ•์€ ์‚ฌ์šฉํ•  ๋ถ€๋ชจํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋ฅผ ํ™•์žฅ๋œ ํด๋ž˜์Šค์˜ constructor ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋„˜๊ฒจ์ฃผ๊ณ  super()์—๋„ constructor ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ™์ด ๋„˜๊ฒจ์ค€๋‹ค.(super() = ๋ถ€๋ชจํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ํ•จ์ˆ˜)

constructor(hddCapacity) { super(hddCapacity) }

1,3 ๋ฒˆ์—์„œ superํ‚ค์›Œ๋“œ๋กœ ๋ถ€๋ชจ์˜ public ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•˜๋ฉด undefined๋ฅผ returnํ•œ๋‹ค. public ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์ผ์ง€๋ผ๋„, superํ‚ค์›Œ๋“œ๋กœ๋Š” ์ƒ์œ„ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„๋ณ€์ˆ˜๋ฅผ ์ง์ ‘ ํ˜ธ์ถœํ•ด ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์—†๋‹ค. ํ•˜์ง€๋งŒ this ๋Š” ์ง์ ‘ ํ˜ธ์ถœ ๊ฐ€๋Šฅ ํ•˜๋‹ค. ์ƒ์œ„ํด๋ž˜์Šค์˜ private ๋ฉค๋ฒ„๋ณ€์ˆ˜์˜ ์ ‘๊ทผ๊ณผ ์ˆ˜์ •์€ getter ์™€ setter๋กœ ๊ฐ€๋Šฅํ•˜๋‹ค.

์ •๋ฆฌ

superthis
parent pubic ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ ์ง์ ‘ ์ ‘๊ทผโŒ (getter/๋ฉค๋ฒ„ ๋ฉ”์„œ๋“œ๋กœ ์ ‘๊ทผ)โœ…
parent pubic ๋ฉค๋ฒ„ ํ•จ์ˆ˜ ์ง์ ‘ ์ ‘๊ทผโœ…โœ…
parent private ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ ์ง์ ‘ ์ ‘๊ทผโŒ ( ๋ถ€๋ชจ ํด๋ž˜์Šค์— setter์žˆ์œผ๋ฉด ๊ฐ€๋Šฅ)โŒ ( ๋ถ€๋ชจ ํด๋ž˜์Šค์— setter์žˆ์œผ๋ฉด ๊ฐ€๋Šฅ)

Class ๋’ค์˜ [ [ prototype ] ] ์ฒด๊ณ„

์œ„์—์„œ ์ƒ์† ๊ฐœ๋…์„ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ class๋ฅผ ์ด์šฉํ•˜์˜€๋‹ค. ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉ๋œ class๋ผ๋Š” ํ‚ค์›Œ๋“œ๋Š” ์‚ฌ์‹ค [[prototype]] ์ฒด๊ณ„์— ๊ธฐ๋ฐ˜์„ ๋‘๊ณ  ์žˆ๋‹ค. You Don't know Js ์˜ ์ €์ž์ธ ์นด์ผ ์‹ฌ์Šจ์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์ฒด๊ณ„๋ฅผ ๋งŽ์€ ๊ฐœ๋ฐœ์ž๋“ค์ด ํด๋ž˜์Šค ๋””์ž์ธ ํŒจํ„ด์œผ๋กœ ์‹คํ˜„ํ•˜๋ ค๋Š” ์š•๊ตฌ๋ฅผ ์ถฉ์กฑํ•˜๊ธฐ ์œ„ํ•ด Syntatic sugar๋ฅผ ์จ๊ฐ€๋ฉฐ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์ฒด๊ณ„๋ฅผ ์–ต์ง€๋กœ ๊ณ ์นœ๊ฒƒ์— ๋ถˆ๊ณผํ•˜๋‹ค๊ณ  ๋งํ•œ๋‹ค. ๊ทธ ์ด์œ ์ค‘ ํ•˜๋‚˜๊ฐ€ ์•„๋งˆ ์ด๋Ÿฐ ๋…ผ๋ฆฌ๋กœ ๋’ท๋ฐ›์นจ ๋  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค. ํด๋ž˜์Šค์ง€ํ–ฅ ์–ธ์–ด์—์„œ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šคํ™”๋Š” ํด๋ž˜์Šค์˜ ๋‚ด๋ถ€๋ฅผ ์ธ์Šคํ„ด์Šค๋กœ ๋ณต์‚ฌํ•œ๋‹ค. ์ด๋Ÿฐ๊ณผ์ •์€ ๋งค ์ธ์Šคํ„ด์Šค๋งˆ๋‹ค ์ผ์–ด๋‚œ๋‹ค. ํ•˜์ง€๋งŒ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ์ด๋Ÿฐ ๋ณต์‚ฌ๊ณผ์ •์ด ์—†๊ณ , [[prototype]] ๋งํฌ๋กœ ๊ฐ์ฒด๋“ค์„ ์—ฐ๊ฒฐํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ๊น ํด๋ž˜์Šค ์ง€ํ–ฅ ์–ธ์–ด์—์„œ์˜ ์ƒ์†์˜ ๊ฐœ๋…๊ณผ ์Šคํฌ๋ฆฝํŠธ ์–ธ์–ด์˜ ๋™์  ํŠน์„ฑ๊ณผ ํ˜ผํ•ฉ๋˜์—ˆ๋‹ค๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ƒ์†(inherit)๋ณด๋‹ค๋Š” ์œ„์ž„(delegation)์ด๋ž€ ๋ง์ด ๋” ๊ฐ€๊น๋‹ค.

์œ„์—์„œ ์„ค๋ช…ํ•œ ๋‚ด์šฉ์„ ๋ฏธ๋ฃจ์–ด๋ณผ ๋•Œ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„  ํ”„๋กœํ† ํƒ€์ž… ๋งํฌ๋กœ ๊ฐ์ฒด๋ฅผ ์—ฐ๊ฒฐํ•œ๋‹ค๊ณ  ํ–ˆ๊ณ  class๋ž€ ํ‚ค์›Œ๋“œ๋Š” ์ „ํ†ต์  ํด๋ž˜์Šค์ง€ํ–ฅ ์–ธ์–ด์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ์ž‘๋™ํ•œ๋‹คํ–ˆ์œผ๋‹ˆ, ์ด ๋ง์„ ํ•ฉ์น˜๋ฉด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋งํ•˜๋Š” ํด๋ž˜์Šค๋Š” ํ”„๋กœํ† ํƒ€์ž… ๋งํฌ๋กœ ๊ฐ์ฒด๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ดํ•ด ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿผ ์—ฌ๊ธฐ์„œ ๋“œ๋Š” ์˜๋ฌธ์ ์€ new ํ‚ค์›Œ๋“œ๋กœ ๊ฐ์ฒด(์ธ์Šคํ„ด์Šค)๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋‚˜ ํด๋ž˜์Šค์ง€ํ–ฅ ์–ธ์–ด์—์„œ๋‚˜ ๋˜‘๊ฐ™์ด ์‚ฌ์šฉํ•˜๋Š”๋ฐ ์–ด๋–ป๊ฒŒ ๋‚ด๋ถ€ ๋™์ž‘์›๋ฆฌ๊ฐ€ ๋‹ค๋ฅธ๊ฐ€์ด๋‹ค.

function Foo(name) { this.name = name console.log(this) // Foo()-> global / new Foo() -> Foo {name:'kelly'} } /*===============================*/ //new ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด, ๊ทธ๋ƒฅ ํ•จ์ˆ˜ํ˜ธ์ถœ let b = Foo('kelly') console.log(b) //undefined console.log(b.name) //undefined /*===============================*/ //new ์‚ฌ์šฉํ•˜๋ฉด ์ƒ์„ฑ์ž ํ˜ธ์ถœ let a = new Foo('kelly') console.log(a.name) //kelly console.log(a.__proto__) //Foo {} console.log(a.prototype) //undefined <-- โ€ผ๏ธ ์ผ๋ฐ˜ ๊ฐ์ฒด์—๋Š” ์ƒ์„ฑ๋˜์ง€ ์•Š๋Š”๋‹ค

new ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒƒ๊ณผ ์•„๋‹Œ๊ฒƒ์„ ๋น„๊ตํ•œ๊ฒƒ์ด๋‹ค. new ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „๊นŒ์ง€ Foo( ) ๋Š” ์ผ๋ฐ˜ํ•จ์ˆ˜์ด๋‹ค. ๊ทธ๋ž˜์„œ this ๋˜ํ•œ ๊ธ€๋กœ๋ฒŒ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋‹ค. ํ•˜์ง€๋งŒ new Foo( ) ๋กœ ํ˜ธ์ถœํ•˜๊ฒŒ ๋˜๋ฉด Foo() ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ๋œ๋‹ค. ์ฆ‰, new ํ‚ค์›Œ๋“œ๋กœ Foo()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด, ์ƒˆ๋กœ์šด ๋นˆ ๊ฐ์ฒด๊ฐ€ ๋งŒ๋“ค์–ด์ง€๊ณ  ์ด ๊ฐ์ฒด(a)๋Š” [[prototype]] ๋ผ๋Š” ๋‚ด๋ถ€ ํ”„๋กœํผํ‹ฐ๊ฐ€ ์ƒ๊ธฐ๊ณ ,[[prototype]] ๋Š” ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋ ˆํผ๋Ÿฐ์Šค๋กœ ์‚ฌ์šฉ๋œ๋‹ค. ์ด๋ ‡๊ฒŒ ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋Š” ํ•ด๋‹น ํ•จ์ˆ˜ this๋กœ ๋ฐ”์ธ๋”ฉ์ด ๋œ๋‹ค. ์œ„์—์„œ ์–ธ๊ธ‰๋œ [[prototype]] ๋ผ๋Š” ๋‚ด๋ถ€ ํ”„๋กœํผํ‹ฐ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋ชจ๋“  ๊ฐ์ฒด๋Š” [[prototype]] ์ด๋ผ๋Š” ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง„๋‹ค (Chrome/vsCode/WebStorm etc ์—์„œ๋Š” proto ). ๋ฐ‘์˜ ์ด๋ฏธ์ง€๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด [[prototype]] ๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ๋‹ค.

oop1

ย  [[prototype]]์ด ์–ด๋–ค ์—ญํ• ์„ ํ•˜๋Š”์ง€ ์ข€ ๋” ์ž์„ธํ•˜๊ฒŒ ์‚ดํŽด๋ณด์ž.

function Foo(name, menu) { this.name = name } let bar = new Foo('kelly') console.log(bar.hasOwnProperty('name')) //true console.log(bar.__proto__ === Foo.prototype) // true console.log(bar.constructor === Foo.prototype.constructor) // true console.log(Foo.prototype.constructor === Foo) console.log(bar.constructor === Foo)

์ƒ์„ฑ๋œ ๊ฐ์ฒด(bar)์˜ name ํ”„๋กœํผํ‹ฐ ์œ ๋ฌด๋ฅผ ํ™•์ธํ•˜๋Š” hasOwnProperty('name' ) ๋Š” true๋ฅผ ๋ฐ˜ํ™˜ํ–ˆ๋‹ค. ์ผ๋‹จ ์—ฌ๊ธฐ์„œ bar ๊ฐ์ฒด์˜ hasOwnProperty() ๋ฉ”์„œ๋“œ๋ฅผ ์„ ์–ธ ํ•œ ์ ์ด ์—†์Œ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์—๋Ÿฌ๊ฐ€ ๋‚˜์ง€์•Š๊ณ  ํ•จ์ˆ˜๊ฐ€ ๋™์ž‘ํ•˜๋Š” ์ด์œ ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์€ bar๊ฐ์ฒด์— hasOwnProperty() ๋ฉ”์„œ๋“œ๊ฐ€ ์—†์œผ๋ฉด ์ด ๊ฐ์ฒด์˜ [[prootype]] (๋‚ด๋ถ€ ๋งํฌ) ์— ์˜ํ•ด ์—ฐ๊ฒฐ ๋˜์–ด์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ๋”ฐ๋ผ๊ฐ€๋ฉด์„œ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ฐพ์„ ๋•Œ๊นŒ์ง€ ์œ ๋ฌด๋ฅผ ํ™•์ธํ•œ๋‹ค. ์ด๊ฒŒ ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ด๋‹(Prototype Chaining)์ด๋ฉฐ ์ผ๋ จ์˜ ๋งํฌ๋“ค์„ ํ”„๋กœํ† ํƒ€์ž… ์—ฐ์‡„(prototype chain)๋ผ๊ณ  ํ•œ๋‹ค. ๊ทธ๋Ÿผ ์ด๋Ÿฐ ์—ฐ์‡„์˜ ๋์€ ์–ด๋””์ผ๊นŒ? hasOwnProperty๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ ์žˆ๋Š” ๊ฐ์ฒด Object.prototype ์ด๋‹ค. JavaScript์—์„œ ๊ฑฐ์˜ ๋ชจ๋“  ๊ฐ์ฒด๋Š” Object์˜ ์ธ์Šคํ„ด์Šค์ด๋ฏ€๋กœ, Object.prototype ์—์„œ ์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์†๋ฐ›๋Š”๋‹ค. ๋‹ค์ด์–ด๊ทธ๋žจ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ์ด๋Ÿฐ ์—ฐ์‡„๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค.

oop2

๊ฐ์ฒด bar๊ฐ€ ์ƒ์„ฑ๋˜๋ฉด ๋‚ด๋ถ€์ ์œผ๋กœ [[prototype]] ํ”„๋กœํผํ‹ฐ๋Š” Foo()ํ•จ์ˆ˜์˜ prototype ํ”„๋กœํผํ‹ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๊ฒŒ ๋œ๋‹ค. ์ฐธ๊ณ ๋กœ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์ฒด ์ƒ์„ฑ ๊ทœ์น™์ธ ํ•จ์ˆ˜ ๊ฐ์ฒด๋Š” length ์™€ prototype์ด๋ผ๋Š” ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฐ€์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•จ์ˆ˜๊ฐ์ฒด ๋˜ํ•œ ๊ฐ์ฒด์ด๋ฏ€๋กœ [[prototype]] ํ”„๋กœํผํ‹ฐ๋„ ํฌํ•จํ•œ๋‹ค.{' '}

์ด๋Ÿฌํ•œ ์ƒ์„ฑ๊ทœ์น™์— ์˜ํ•ด Foo.prototype์— ์ ‘๊ทผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Foo.prototype ๋˜ํ•œ ๊ฐ์ฒด์ด๋ฏ€๋กœ constructor ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. .prototype ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ๊ฐ€์ง„ ํ•จ์ˆ˜, ์ฆ‰ ์œ„์˜ ์˜ˆ์ œ์—์„œ๋Š” Foo() ํ•จ์ˆ˜๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

constructor์— ๋Œ€ํ•ด ์กฐ๊ธˆ ๋” ์•Œ์•„๋ณด์ž. ์œ„ ์ฝ”๋“œ๋ฅผ ์กฐ๊ธˆ ๋” ํ™•์žฅํ–ˆ๋‹ค.

function Foo(name, menu) { this.name = name } console.log(Foo.prototype.constructor == Foo) //true let bar = new Foo('kelly') console.log(bar.constructor === Foo) //true

ํด๋ž˜์Šค์ง€ํ–ฅ์—์„œ ๊ทธ๋ ‡๋“ฏ์ด constructor๋Š” new ํ‚ค์›Œ๋“œ๋กœ ํ˜ธ์ถœ ๋˜์–ด ์ง€๊ณ  ์ธ์Šคํ„ด์Šค๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค๊ณ  ์•Œ๊ณ ์žˆ๋‹ค. ์œ„์˜ bar ๊ฐ์ฒด๋„ Foo์— ์˜ํ•ด ์ƒ์„ฑ๋˜์—ˆ์œผ๋‹ˆ, constructor๊ฐ€ ํด๋ž˜์Šค์ง€ํ–ฅ์—์„œ ์ƒ๊ฐํ•˜๋Š” ์ƒ์„ฑ์ž๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์นด์ผ์‹ฌ์Šจ์€ "์ƒ์„ฑ์ž์™€ ํ”„๋กœํ† ํƒ€์ž…์ด๋ž€ ์šฉ์–ด์ž์ฒด์˜ ์˜๋ฏธ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋Š์Šจํ•˜๊ธฐ ์ง์ด ์—†์–ด์„œ ๋‚˜์ค‘์—๋Š” ๋ถ€ํ•ฉํ•˜์ง€ ์•Š์„ ๊ฐ€๋Šฅ์„ฑ๋„ ์žˆ๋‹ค. ์ƒ์„ฑ์ž๋Š” '์ƒ์„ฑ๋จ์„ ์˜๋ฏธํ•˜์ง€ ์•Š๋Š”๋‹ค.'" ๋ผ๊ณ  ๋งํ•œ๋‹ค. ์ฆ๋ช…์„ ๋ณด๊ฒ ๋‹ค. ์œ„์˜ ์ฝ”๋“œ์—์„œ bar.constructor === Foo ๊ฐ€ true๋ฅผ ๋ฆฌํ„ดํ•˜์˜€์œผ๋ฏ€๋กœ Foo()๊ฐ€ bar๋ฅผ ์ƒ์„ฑํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

console.log(Foo.prototype.constructor == Foo) //true Foo.prototype = { age: 20, } let bar = new Foo('kelly') console.log(bar.constructor === Foo) //false

ํ•˜์ง€๋งŒ, Foo.prototype ์— ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ์ •์˜ ํ•˜๋Š” ์ˆœ๊ฐ„, bar์˜ constructor ๋Š” ์—†์–ด์ง„๋‹ค. ๋”์ด์ƒ Foo๊ฐ€ ์•„๋‹ˆ๋‹ค. ์™œ ๊ทธ๋Ÿด๊นŒ?

constructor๊ฐ€ bar๊ฐ์ฒด ๋‚ด์— ์‹ค์ œ ์กด์žฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, Foo.prototype ๊ฐ์ฒด์— ์œ„์ž„ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, Foo.prototype ์„ ์ƒˆ๋กœ์šด ๊ฐ์ฒด, ๋‹ค๋ฅธ ํ”„๋กœํผํ‹ฐ๋“ค๊ณผ ํ•จ๊ป˜ ๋ฐ”๋€Œ๋ฉด, constructor ํ”„๋กœํผํ‹ฐ๊ฐ€ ๋‹น์—ฐํžˆ ์—†์–ด์ง€๋ฏ€๋กœ bar ๊ฐ์ฒด๊ฐ€ ๋‹น์—ฐ ์ ‘๊ทผํ•  ์ˆ˜๊ฐ€ ์—†๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ด๋Ÿฐ ๋…ผ๋ฆฌ์— ์˜ํ•˜๋ฉด, bar.constructor๊ฐ€ ์‹ค์ œ ๊ธฐ๋ณธํ•จ์ˆ˜๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋ ˆํผ๋Ÿฐ์Šค๋ผ๋Š” ๋ณด์žฅ์ด ์ „ํ˜€ ์•ˆ๋˜๋ฏ€๋กœ ๋ถˆ์•ˆ์ • ํ•  ๋ฟ๋”๋Ÿฌ, ์ง์ ‘ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹๋‹ค๊ณ  ํ•œ๋‹ค.

๋‘ ๊ฐ์ฒด๊ฐ„์˜ ์œ„์ž„ ๋งํฌ ์ƒ์„ฑ

function Vehicle(numberOfWheels) { this.numberOfWheels = numberOfWheels } Vehicle.prototype.getNumberOfWheel = function () { return this.numberOfWheels } function Car(arg) {} Car.prototype = Object.create(Vehicle.prototype) const tesla = new Car(4) console.log(tesla.getNumberOfWheel()) //undefined console.log(tesla.constructor === Car.prototype) //false

์ด๋ฒˆ์ฝ”๋“œ๋Š” ์œ„์—์„œ ์–ธ๊ธ‰ํ–ˆ๋˜ prototype์— ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ํ• ๋‹นํ–ˆ๋˜ ๊ฒƒ๊ณผ ๋น„์Šทํ•œ ์˜ˆ์ œ์ด๋‹ค. ๋Œ€์‹  object๋ฅผ ์ƒ์„ฑํ•˜๋Š”๋ฐ ๊ฐ์ฒด๋ฆฌํ„ฐ๋Ÿด์ด ์•„๋‹Œ ES05์˜ Object.create() ๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋‹ค. Car.prototype์€ ์ƒˆ๋กœ์šด ๊ฐ์ฒด vehicle.prototype ์„ ์—ฐ๊ฒฐํ•œ๋‹ค. ์–ด๋–ป๊ฒŒ? car.prototype ์€ ๊ฐ์ฒด์ด๋ฏ€๋กœ, prototype ํ”„๋กœํผํ‹ฐ๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๊ณ , ์ด ๋‚ด๋ถ€๋งํฌ๋Š” Object.create()์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์ธ vehicle.prototype ๊ณผ ์—ฐ๊ฒฐํ•œ๋‹ค. ๊ทธ๋Ÿผ Car()์— ์˜ํ•ด ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค tesla๋Š” Vehicle ๊ฐ์ฒด์˜ getNumberOfWheel()๋ฉ”์„œ๋“œ์— ์ ‘๊ทผํ•ด๋„ ๋˜์ง€ ์•Š์„๊นŒ? ์ ‘๊ทผ์„ ํ•˜๋ฉด undefined๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์œ„์—์„œ ์–ธ๊ธ‰ํ•˜์˜€๋“ฏ์ด Car.prototype์— ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ํ• ๋‹นํ•จ์œผ๋กœ์จ tesla์˜ constructor๋Š” ๋” ์ด์ƒ Car.prototype ๊ณผ์˜ ์—ฐ๊ฒฐ์ด ๋Š์–ด์ ธ Vehicle ๊ฐ์ฒด์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋‚˜ ๋ฉ”์„œ๋“œ์— ์ ‘๊ทผ์„ ๋ชปํ•œ๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ call ๋˜๋Š” apply๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

function Vehicle(numberOfWheels) { this.numberOfWheels = numberOfWheels this.engine = engine } Vehicle.prototype.getNumberOfWheel = function () { return this.numberOfWheels } function Car(arg) { Vehicle.call(this, arg) } Car.prototype = Object.create(Vehicle.prototype) Car.prototype.identify = function () { console.log(`This car has ${this.getNumberOfWheel()} wheels.`) } const tesla = new Car(4) console.log(tesla.getNumberOfWheel()) tesla.identify() //This car has 4 wheels.

call์€ Vehicle ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œ์ผœ ์ฒซ๋ฒˆ์งธ ์ธ์ž์ธ this ๊ทธ๋Ÿฌ๋‹ˆ๊น Car์˜ ์ธ์Šคํ„ด์Šค์— this๋ฅผ ๋ฐ”์ธ๋”ฉํ•œ๋‹ค. tesla๋ฅผ callํ•˜๊ธฐ์ „ ๊ณผ ํ›„๋ฅผ ๋น„๊ตํ•ด๋ณด๋ฉด,

  • call() ํ•˜๊ธฐ ์ „
tesla2 * call() ์ธํ•œ ๋ฐ”์ธ๋”ฉ ํ›„{' '} tesla1

Car์˜ ์ธ์Šคํ„ด์Šค์ธ tesla๋Š” Vehicle ํ•จ์ˆ˜ ๊ฐ์ฒด์˜ ๋ณ€์ˆ˜๋‚˜ ๋ฉ”์„œ๋“œ์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ ์ด๊ฒƒ์€ ๋งˆ์น˜ ํด๋ž˜์Šค ์ง€ํ–ฅ์–ธ์–ด์—์„œ์˜ ์ƒ์†๊ณผ ๊ฐ™์€ ์–‘์ƒ์„ ๋ˆ๋‹ค. ํ•˜์ง€๋งŒ ์—ฌ๊ธฐ์„œ ์‹ ๊ฒฝ์„ ์จ์ค˜์•ผ ํ•  ์ ์€ Car.prototype ์— ์ƒˆ ๊ฐ์ฒด๋ฅผ ํ• ๋‹นํ•œ ํ›„๋กœ Car.prototype.constructor๊ฐ€ ์—†์–ด์กŒ์œผ๋ฏ€๋กœ, Car.prototype์— ์ถ”๊ฐ€ํ•  ๋ฉ”์„œ๋“œ๋‚˜ ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋‹ค๋ฉด, ์ˆ˜๋™์œผ๋กœ ํ•ด๊ฒฐํ•ด์•ผ ํ•œ๋‹ค. ES06์—์„  Object.setPrototypeOf(), Object.getPrototypeOf() ์„ ์‚ฌ์šฉํ•˜์—ฌ prototype ์œผ๋กœ prototype ๊ฐ์ฒด๋“ค์„ ์—ฐ๊ฒฐ ํ•  ์ˆ˜ ์žˆ๋‹ค. MDN์˜ prototype ์„ค๋ช…์— ๋”ฐ๋ฅด๋ฉด, __proto__ ์€ deprecated ๋˜์—ˆ๋‹ค๊ณ  ๋‚˜์™€์žˆ๋‹ค. (Last modified: 2021๋…„ 6์›” 12์ผ)

prototype ํšจ์œจ์„ฑ

ํ•จ์ˆ˜๊ฐ์ฒด์˜ prototype์„ ์ด์šฉํ•˜์—ฌ ์ƒ์†์„ ๊ตฌํ˜„ํ• ๊ฒฝ์šฐ ๊ณตํ†ต์ ์œผ๋กœ ์‚ฌ์šฉ๋˜์–ด์ง€๋Š” ํด๋ž˜์Šค ๋ฉค๋ฒ„์— ๋Œ€ํ•ด ์ž์›๋‚ญ๋น„๋ฅผ ์ค„์ผ ์ˆ˜๊ฐ€ ์žˆ๋Š”๋ฐ ์˜ˆ๋ฅผ ๋“ค๋ฉด ์ด๋ ‡๋‹ค.

function Student(name, years) { this.name = name this.years = years this.getName = function () { return this.name } this.setName = function (arg) { this.name = arg } } let student1 = new Student('sue', 'sophomore') let student2 = new Student('kelly', 'sophomore') let student3 = new Student('kyle', 'junior') console.log(student1.getName()) //sue console.dir(student1) console.dir(student2) console.dir(Student) function Student(name, years) { this.name = name this.years = years } Student.prototype.setName = function () { this.name = value } Student.prototype.getName = function () { return this.name } let student1 = new Student('sue', 'sophomore') let student2 = new Student('kelly', 'sophomore') let student3 = new Student('kyle', 'sophomore') console.log(student1.getName()) //sue console.dir(student1) console.dir(student2) console.dir(Student)

์ฒซ๋ฒˆ์งธ ์ฝ”๋“œ๋ฒ ์ด์Šค์™€ ๋‘๋ฒˆ์งธ ์ฝ”๋“œ๋ฒ ์ด์Šค์˜ ์ฐจ์ด์ ์€ ํ•จ์ˆ˜๊ฐ์ฒด ๋‚ด์— ๋ฉค๋ฒ„ ๋ฉ”์„œ๋“œ๋ฅผ ์„ ์–ธํ•  ๋•Œ, this๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•จ์ˆ˜๊ฐ์ฒด ๋‚ด์— ์„ ์–ธํ•˜๋Š”๊ฐ€์™€ ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋ฅผ ํ•จ์ˆ˜๊ฐ์ฒด scope ๋ฐ–์— ๋”ฐ๋กœ Student.prototype ์— ์ฒด์ด๋‹์œผ๋กœ ์ ‘๊ทผํ•˜๋ƒ์ด๋‹ค. ๋ณด์—ฌ์ง„ ์˜ˆ์‹œ์—์„œ๋Š” Student ๊ฐ์ฒดํ•จ์ˆ˜์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ 3๊ฐœ ๋ฟ์ด์ง€๋งŒ ์ธ์Šคํ„ด์Šค๊ฐ€ 1000๊ฐœ๋ผ๋ฉด, ๋งค ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ์‹œ, ๊ฐ ์ธ์Šคํ„ด์Šค ๋งˆ๋‹ค getName ,setName์„ ์ƒ์„ฑํ•˜๊ฒŒ ๋œ๋‹ค. ์–ด๋–ค ์ธ์Šคํ„ด์Šค๋Š” getName ๊ณผ setName ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋‹ค. ์ด๋Ÿฐ ์‹œ๋‚˜๋ฆฌ์˜ค๋กœ ๋ฏธ๋ฃจ์–ด ๋ณด์•„ ๋ถˆํ•„์š”ํ•œ ์ค‘๋ณต๋œ ์ž์›๋“ค์ด ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐˆ ๊ฒƒ์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ์ƒํ™ฉ์„ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์ด prototype์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด๊ณ  ๋‘๋ฒˆ์งธ ์ฝ”๋“œ๊ฐ€ ์˜ˆ์‹œ์ด๋‹ค. Student์˜ ํ•จ์ˆ˜๊ฐ์ฒด์˜ ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด์— ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋ฅผ ์ •์˜ ํ•˜์—ฌ ํ™•์žฅ์‹œํ‚จ๋‹ค.(Student.prototype.setName )


prototype memory Page 2
๐Ÿ‘†this๋ฅผ ์‚ฌ์šฉํ•œ ํด๋ž˜์Šค ๋ฉค๋ฒ„ ํ•จ์ˆ˜

prototype memory Page 3
๐Ÿ‘† ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ด๋‹

์œ„์˜ ๋‹ค์ด์–ด๊ทธ๋žจ์—์„œ ๋ณด์—ฌ์ฃผ๋“ฏ์ด, Student ํ•จ์ˆ˜๊ฐ์ฒด๋กœ ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค๋“ค์€ ๋ชจ๋‘ Student.prototype์„ ๊ฐ€๋ฆฌํ‚ค๊ธฐ ๋•Œ๋ฌธ์— prototype ์ฒด์ด๋‹์œผ๋กœ Student.prototype์˜ ๋ฉค๋ฒ„ํ•จ์ˆ˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ฒฐ๊ตญ Prototype ์—์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ์˜ ์ƒ์†์˜ ๊ฐœ๋…์€ ํด๋ž˜์Šค๊ฐ์ฒด ์ง€ํ–ฅ ์–ธ์–ด์—์„œ์˜ ์ƒ์†๊ณผ๋Š” ๋‹ค๋ฅธ ์„œ๋กœ์˜ ๊ฐ์ฒด๊ฐ„์˜ ์ฒด์ด๋‹์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ทธ ์ด๋ก ์— ๋Œ€ํ•œ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์„ค๋ช…์œผ๋ก , new ๋กœ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ, ๊ฒฐ๊ตญ ๊ฐ์ฒด์™€ ๊ฐ์ฒด๋ฅผ ์—ฐ๊ฒฐํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ž˜์„œ ๊ฒฐ๊ตญ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ class๋„ ํ”„๋กœํ† ํƒ€์ž…์˜ ์œ„์ž„์ฒด๊ณ„์— ๊ธฐ๋ฐ˜์„ ๋‘” ์ผ์ข…์˜ ๊ฐ„ํŽธ๊ตฌ๋ฌธ์ด๋‹ค.


์ฐธ๊ณ ์ž๋ฃŒ

  • [๋„์„œ - ์นด์ผ ์‹ฌ์Šจ, YOU DON'T KNOW JS, ํ•œ๋น› ๋ฏธ๋””์–ด]
  • [๋„์„œ - ์†กํ˜•์ฃผ/๊ณ ํ˜„์ค€, ์ธ์‚ฌ์ด๋“œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ, ํ•œ๋น› ๋ฏธ๋””์–ด ]
  • [๋„์„œ - ์ •์ง„์šฑ, Quick start TypeScript, ๋ฃจ๋น„ํŽ˜์ด]
  • MDN Prototype

Hi, I'm Hyunsu ๐Ÿ‘‹

Profile Image

์•ˆ๋…•ํ•˜์„ธ์š”. ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž ์ฃผํ˜„์ˆ˜์ž…๋‹ˆ๋‹ค.

๊ฐœ๋ฐœ์„ ํ†ตํ•ด ์‚ฌ์šฉ์ž๋“ค์—๊ฒŒ ํ’๋ถ€ํ•˜๊ณ  ๊ฐ€์น˜ ์žˆ๋Š” ๊ฒฝํ—˜์„ ์ œ๊ณตํ•˜๋Š” ์ผ์— ๋ฟŒ๋“ฏํ•จ์„ ๋Š๋‚๋‹ˆ๋‹ค.

์˜ต์‹œ๋””์–ธ(Obsidian)์—์„œ ํ˜„์žฌ ๋ธ”๋กœ๊ทธ๋กœ ํ•˜๋‚˜์”ฉ ๊ธ€์„ ์˜ฎ๊ธฐ๋Š” ๊ณผ์ •์— ์žˆ์–ด์š”. โ˜•๏ธ ๐Ÿ‘ฉโ€๐Ÿ’ป โ›ท

Github on ViewReach Me Out