클래스는 하나의 원본이 되는 청사진
(함수를 작성하여 객체를 정의)
예시)
class Smartphone {
constructor(brand,name,color){
...
}
}
인스턴스는 클래스에서 파생되는 객체
(정의된 객체를 가지고 인스턴스 생성)
예시)
let iphone = new Smartphone('apple','iphone','silver');
let galaxy = new Smartphone('smasung','galaxy','black');
let xperia = new Smartphone('sony','xperia','green');
변수에 클래스의 설계를 가진 새로운 객체, 인스턴스가 할당됨.
이때
1. 빈 객체를 만들어 this에 할당
2. 함수 본문을 실행합니다. this에 새로운 프로퍼티를 추가해 this를 수정
3. this를 반환
객체 생성의 함수
모양은 일반함수와 같으나 , new키워드를 사용하지 않으면 일반함수와 동일하게 작동함
function Smartphone(brand,name,color){
...
}
class Smartphone {
constructor(brand,name,color){
...
}
}