function Car() {
this.wheel = 4;
this.beep = "BEEP";
}
Car.prototype.go = function () {
alert(this.beep);
};
function Truck() {
this.wheel = 6;
this.beep = "TRUCK!!";
}
Truck.prototype = new Car();
function SUV() {
this.beep = "SUV!!";
}
SUV.prototype = new Car();
var truck = new Truck(),
suv = new SUV();
console.log(truck.wheel); // 6
console.log(suv.wheel); // 4
console.log(truck.beep); // TRUCK!!
console.log(suv.beep); // SUV!!
Car라는 객체를 프로토타입에 넣어보니Truck과 Suv 는 go라는 함수를 공통으로 사용할 수 있으며, 특히 SUV의 경우 wheel을 정의하지 않았지만
Car에 정의되어 있는 wheel = 4로 바퀴갯수 프로퍼티를 가질 수 있게 되었다.
댓글 없음:
댓글 쓰기