" JavaScript is prototype
based Object oriented language. "
Object Oriented Programming
"데이터를 추상화시켜상호작용(interaction)하는 객체들의 집합"
1. 자바스크립트는 프로토타입 기반
언어
클래스로 객체 자료구조, 기능 정의하고 생성자를 통해 인스턴스 생성
클래스 : 같은 종류 집단에 속하는 속성(attribute), 행위(behavior) 정의한 것
인스턴스 : 오직 클래스 정의된 범위 내에서 작동 (정확성, 안정성, 예측성)
ex) java, python, c#, php
클래스 개념이 없음
이미 생성된 인스턴스의 자료구조, 기능 동적으로 변경 가능
생성자 함수는 js에서 클래스이자 생성자 역할
프로토타입 객체: 모든 객체는 부모 역할 담당하는 객체와 연결
var 변수처럼 호이스팅 발생하지 않습니다.
하지만, let 변수와 같이 블록 내에서 호이스팅 발생합니다.
console.log(Dog); //error!
class Dog{ }
클래스 명은 함수 표현식과 동일하게 클래스 몸체 내부 유효 식별자
const Doggy = class Dog { }
const snoopy = new Doggy() // Dog { }
new Dog() //error!
new 연산자와 함꼐 클래스 호출 시 인스턴스 생성
* new 연산자와 함께 호출한 것은 클래스명이 아니무니다! >>> Constructor 이무니다!
Object.getPrototypeof(snoopy).constructor === Dog
* new 연산자 없이 constructor 호출할 수 없으무니다!
class Dog{
constructor() { ... }
//getter
get getHand(){
return this.hand;
}
}
const snoopy = new Dog();
snoopy.getHand; //snoopy.getHand() ❌
class Dog{
constructor() { ... }
//setter
set setBark(pNum){
return `${pNum}번 멍멍멍`;
}
}
const snoopy = new Dog();
snoopy.setBark = 3; //snoopy.setHand(3) ❌
클래스 인스턴스 생성 없이도 호출 가능한 메서드 (인스턴스 호출 ❌)
정적 메서드는 this 사용 ❌ (this는 클래스 인스턴스 의미, 인스턴스 생성 전제하에 this 사용하므로..)
= this를 사용한 필요없는 메서드 (전역 객체 주로 사용)
class Dog{
static runAway(){
return "FreeDom!!!";
}
}
Dog.runAway()
const snoopy = new Dog();
snoopy.runAway(); //error!!!
부모클래스는 상속 받는 자식 클래스 정의
class Dog{
constructor(pName){
this.name = pName;
this.hand = 'front right hand';
}
getHand(){
return this.hand;
}
setBark(){
return `멍멍멍`;
}
}
class Puppy extends Dog{
construtor(pName,pNum){
super(pName)
this.num = pNum;
}
setBark(){
return `${this.num}번 멍멍멍, ${super.getHand()}도 주세요..!`;
}
}
부모 클래스 참조 or 부모 클래스 호출
class Dog{
constructor(){ ... }
...
}
class Puppy extends Dog{
construtor(props){
super(props);
...
}
...
}
Puppy.[[prototype]] === Dog
Puppy.prototype.[[prototype]] === Dog.prototype