var model = (function(){
var Car = (function(){
var constractor = function Car(color){
this.tireCount = 4;
this.color = color;
return this;
};
var p = constractor.prototype;
p.getColor = function (){
return this.color;
};
p.getTireCount = function (){
return this.tireCount;
};
return constractor;
})();
var Truck = (function(){
var constractor = function Truck(color, load){
this.tireCount = 8;
this.color = color;
this.load = load;
return this;
};
var p = constractor.prototype;
Object.setPrototypeOf(p, Car.prototype); // 継承!
p.getLoad = function (){
return this.load;
};
return constractor;
})();
return {
Car: Car,
Truck: Truck
};
})();
結果
>>> var car = new model.Car("red");
>>> console.log(car);
Car { tireCount: 4, color: 'red' }
>>> console.log(car.getColor());
red
>>> console.log(car.getTireCount());
4
>>> var truck = new model.Truck("blue", ["amazon", "amazon", "rakuten"]);
>>> console.log(truck);
Truck {
tireCount: 8,
color: 'blue',
load: [ 'amazon', 'amazon', 'rakuten' ]
}
>>> console.log(truck.getColor());
blue
>>> console.log(truck.getTireCount());
8
>>> console.log(truck.getLoad());
[ 'amazon', 'amazon', 'rakuten' ]