[21.08.23]클래스를 이용한 모듈화

김정훈·2021년 8월 23일
0

javascript

목록 보기
12/19

1. 클래스와 인스턴스

Object 생성기계인 constructor


ES6 class 키워드로 구현하는 constructor 만들기
class Car {
  constructor(brand, name, color){
    this.brand = brand;
    this.name = name;
    this.color = color;
  }
  refuel(){
  	console.log('기름을 넣었습니다.')
  }
}
let avante = new Car('h', 'avante', 'red);

this : 기계에서 새로생성되는 object(인스턴스)
Car : object 생성기계(constructor, 생성자)
상속 : avante가 Car가 가지고 있는 brand, name 등을 물려받음


2. prototype

prototype = 유전자!


부모에 prototype에 등록하면 자식이 물려받음 = 유전자와 같음
* 부모의 prototype확인
  - Car.prototype
  
* 부모의 prototype추가
  - Car.prototype.date = '2021. 03. 02'
  
* 자식의 prototype확인
  - avante.__proto__


3. prototype 동작원리

1. 자식이 인스턴스를 가지고 있니? 
2. 부모 유전자(Car.prototype)가 인스턴스를 가지고 있니?
3. 부모의 부모가 인스턴스를 가지고 있니? ...
이와같은 동작원리를 통해 내장함수(sort(), map(), push()...)가 사용가능함.
let arr = [1, 2, 3]
let arr = new Array(1, 2, 3)와 같음 
Array.prototype을 통해 상속 받기 때문에 내장 함수를 이용가능.


profile
프론트엔드 개발자를 꿈꾸고 있습니다!

0개의 댓글

관련 채용 정보