๐Ÿ’Šย  ์ƒ์†๊ณผ ํ”„๋กœํ† ํƒ€์ž… ( prototype )

๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” 3๊ฐ€์ง€ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋ฅผ ์‚ดํŽด๋ณด์ž ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ person1, person2, person3๋ฅผ ๋” ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์—ฌ๊ธฐ์— name , age๋Š” ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์œผ๋‚˜ sayHello ๋ฉ”์†Œ๋“œ๋ฅผ ๋ชจ๋‘ ๊ฐ™์€ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ด ๋ฉ”์†Œ๋“œ๋ฅผ ํ•˜๋‚˜๋งŒ ์„ ์–ธํ•˜๊ณ  ๋ชจ๋‘ ๊ณต์œ ํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค ์ด๋•Œ sayHello๋ฉ”์†Œ๋“œ๋ฅผ prototype์œผ๋กœ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

function Person(name,age){
	this.name=name;
	this.age = age;
  this.sayHello = function(){
	   console.log(` Hello ${this.name}`);   
  }
};

var person1= new Person('Jane',30)
var person2= new Person('Tom',30)
var person3= new Person('John',30)
console.log(person1)
console.log(person2)
console.log(person3)
Person.prototype.sayHello = function(){
	   console.log(` Hello ${this.name}`);   
  }

๐Ÿ’Šย  ์•„๋ž˜ 2๊ฐœ์˜ ์ฝ”๋“œ๋ฅผ ๋น„๊ตํ•ด ๋ณด์ž

ํŠน๋ณ„ํžˆ ํ• ๋‹นํ•˜์ง€ ์•Š๋”๋ผ๊ณ  ๋ชจ๋“  ํ•จ์ˆ˜๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ โ€œprototypeโ€ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ–๋Š”๋ฐ ๋””ํดํŠธ ํ”„๋กœํผํ‹ฐ prototype์€ constructor ํ”„๋กœํผํ‹ฐ ํ•˜๋‚˜๋งŒ ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋ฉฐ constructor ํ”„๋กœํผํ‹ฐ๋Š” ํ•จ์ˆ˜์ž์‹ ์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ constuctor ํ”„๋กœํผํ‹ฐ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์ด ํ”„๋กœํ† ํƒ€์ž… ์ƒ์„ฑํ• ๋•Œ ์•”๋ฌต์ ์œผ๋กœ ์ถ”๊ฐ€ํ•œ ํ”„๋กœํผํ‹ฐ์ด๋‹ค

const Person =(function (){

  function Person(name){
    this.name = name;
  }
**Person.prototype.sayHello = function(){
    console.log(`Hi, My name is ${this.name}`)
  }**

 return Person;
}());
const me = new Person('Lee');
>Person.prototype 
// {sayHello: ฦ’, constructor: ฦ’}
>Person.constructor === Person  // false
>me.constructor === Person // ture
>me.constructor === Object // false
const Person =(function (){

 function Person(name){
    this.name = name;
  }
**Person.prototype ={
  sayHello(){
    console.log(`Hi, My name is ${this.name}`)
  }**
}
 return Person;
}());
const me = new Person('Lee');
>Person.prototype   // {sayHello: ฦ’}
>Person.constructor === Person // false
**>me.constructor === Person // false
>me.constructor === Object // true**

์˜†์˜ ์ฝ”๋“œ๋Š” ๋””ํดํŠธ prototype๋ฅผ ๋ฎ์–ด์จ์„œ constructor๊ฐ€ ์—†์–ด์ง„๊ฒƒ์ด๋‹ค. ๊ทธ๋ž˜์„œ ์•„๋ž˜์ฒ˜๋Ÿผ ์ฝ”๋“œ๋ฅผ ๋ฐ”๊พธ๋ฉด

Person.prototype ={
  constructor : Person,
  sayHello(){
    console.log(`Hi, My name is ${this.name}`)
  }
}
**>me.constructor === Person // ture  
>me.constructor === Object // fasle๊ฐ€ ๋‚˜์˜จ๋‹ค**

๐Ÿ’Šย extends ๋กœ ์ƒ์„ฑํ•œ ๊ฐ์ฒด์— constructor๋ฅผ ์ƒ์„ฑํ•˜๊ฒŒ ๋˜๋ฉด

class Car{
  constructor(color){
    this.color= color;
    this.wheels =4;
  }
  drive(){
    console.log('DRIVE')
  }
  stop(){
    console.log('STOP')
  }
}

class Bmw extends Car{
  constructor(color){
  // super(color); ๋ถ€๋ชจ๊ฐ์ฒด์˜ ํ˜•์‹๋Œ€๋กœ 
    this.navi = 1; //  ์—๋Ÿฌ
  }

  park(){
    console.log('PARK')
  }
  stop(){
    super.stop();
    console.log('stop...')
  }
}
const x5 = new Bmw('red')

class์—์„œ constructor ์ƒ์„ฑํ• ๋•Œ ๋จผ์ € ๋นˆ ๊ฐ์ฒด { }๋ฅผ ๋งŒ๋“ค๊ณ  this ๋กœ ์ด ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ extends ๋ฅผ ์‚ฌ์šฉํ•œ ๊ฐ์ฒด๋Š” ์ด๊ณผ์ •์„ ๊ฑด๋„ˆ๋›ฐ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋“œ์‹œ super()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ constructor๋ฅผ ์‹คํ–‰ํ•ด์•ผ ํ•œ๋‹ค.

๋งŒ์•ฝ ์ž์‹ ๊ฐ์ฒด์— contructor๊ฐ€ ์—†๋‹ค๋ฉด ์•”๋ฌต์ ์œผ๋กœ ๋ถ€๋ชจ์˜ contructor๋ฅผ ์‹คํ–‰ํ•œ๋‹ค

class Bmw extends Car{
constructor(...args){
	super(...args);
}

๐Ÿ’Šย constructor ๋ฅผ ์žฌ ์ •์˜

function Shape(){
}

Shape.prototype.duplicate = function(){
   console.log('duplicate')
}

function Circle(radius){
   this.radius = radius;
}
// ๋‚˜์ค‘์— ์—ฌ๊ธฐ์— ์ถ”๊ฐ€
Circle.prototype.draw = function(){
   console.log('draw')
}

const s = new Shape()
const c = new Circle(1)