I'm trying to create an object (car1) inside another object (Garage).
car1 object should inherit from Car object and define it's own members. But the code below is not working. Can someone tell me the reason and maybe a solution.
Code:
<script type="text/javascript">
function Car(sColor) {
this.color = sColor;
}
Car.prototype.sayColor = function() {
alert(this.color);
};
var Garage = {
car1: {
prototype: new Car("red"),
owner: "john"
}
};
alert(Garage.car1.owner); // outputs "john"
alert(Garage.car1.color); // outputs "undefined"
alert(Garage.car1.prototype.color); // outputs "red"
Garage.car1.prototype.sayColor(); // outputs "red"
Garage.car1.sayColor(); // fails
</script>
Why wouldn't this kind of inheritance work?
Isn't it (the statement in red) the same as:
Code:
car1.prototype = new Car("red");
EDIT: I even tried this:
Code:
var Garage = {
car1: function() {},
car1.prototype: new Car("red"),
car1.owner: "john"
};
But no use.
Thanks