객체를 만들어내기 위한 설계도 또는 템플릿
class Person {
constructor(name, age) {
this.name = name; // field
this.age = age; // field
}
speak() {
// method
console.log(`${this.name}: hello!`);
}
}
소프트웨어 세계에 구현할 대상, 클래스로부터 만들어진 실체
객체를 실체화한 것, 메모리에 실제로 할당된 구체적인 실체
거의 같은 의미로 쓰이나 강조하는 관점이 다르다고 한다.
// Class 정의
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
speak() {
console.log(`${this.name}: hello!`);
}
}
// Object/Instance 생성
const ellie = new Person("ellie", 20); // ellie는 Person 클래스의 인스턴스
const steve = new Person("steve", 25); // steve는 Person 클래스의 인스턴스
// 사용
console.log(ellie.name); // 'ellie'
ellie.speak(); // 'ellie: hello!'
구분 | Class | Object | Instance |
---|---|---|---|
역할 | 설계도/템플릿 | 구현할 대상 | 실체화된 구체적 객체 |
데이터 | 데이터 없음 | 실제 데이터 포함 | 메모리에 할당된 데이터 |
생성 횟수 | 한 번 선언 | 여러 번 생성 가능 | 여러 번 생성 가능 |
메모리 | 메모리 할당 없음 | 개념적 존재 | 실제 메모리 할당 |