일종의 원형(original form)으로 , 객체를 생성하기 위한 아이디어나 청사진
=== 생성자 함수
클래스의 사례(instance object)
new
로 만듬
this는 생성된 인스턴스 자기 자신 , 인스턴스 객체를 의미, parameter로 넘어온 브랜드 ,이름 ,색상 등은 인스턴스 생성시 지정하는 값
객체의 메서드로 호출될 때, 해당 메서드 내부에서 this
는 그 메서드를 소유하는 객체를 가리킨다.
eg.
class car {
constructor(brand, name, color){
this.brand = brand;
this.name = name;
this.color = color;
}
drive() {
// 메서드
console.log(`${this.brand}의 ${this.name}가 달립니다!`);
}
}
// 위와 같이 this에 할당한다는 것은, 만들어진 인스턴스에 해당 브랜드,이름,색상을 부여하겠다는 의미
// 클래스를 통한 인스턴스(객체) 생성
const myCar = new car('Tesla', 'Model S', 'White');
console.log(myCar.brand); // Tesla
console.log(myCar.name); // Model S
console.log(myCar.color); // White
eg.
const obj = {
name: 'Hello',
getName: function() {
console.log(this.name);
}
};
obj.getName(); // "Hello"