class Car { constructor(brand, model, color) { this.brand = brand; this.model = model; this.color = color; this.speed = 0; } accelerate() {this.speed += 10;} brake() {this.speed -= 10;} } const newCar = new Car("Hyundai", "Sonata", "Black");
.__proto__
'.__proto__'는 객체의 프로토타입에 접근하기 위한 속성이다.
.__proto__
속성을 가지고 있으며, 해당 객체의 프로토타입 객체를 가리킨다.newCar.__proto__;
위의 결과
Car { constructor(brand, model, color) { this.brand = brand; this.model = model; this.color = color; this.speed = 0; } accelerate() { this.speed += 10; } brake() { this.speed -= 10; } }
.prototype
'.prototype'은 함수 객체에만 존재하는 속성이다.
Car.prototype;
위의 결과
Car { constructor(brand, model, color) { this.brand = brand; this.model = model; this.color = color; this.speed = 0; } accelerate() { this.speed += 10; } brake() { this.speed -= 10; } }
세가지의 관계는 다음과 같이 생각할 수 있다.
클래스 : 자동차의 완제품(인스턴스)를 만들기 위해 기초가 되는 뼈대
인스턴스 : 뼈대(클래스)를 기반으로 만들어진 완성품 자동차
프로토타입 : 완성품 자동차(인스턴스)의 뼈대(클래스)에 저장된 속성이나 메서드를 찾는 경로