In the first example, you initialize your object Carto have all the same properties of the new object Vehicle. However, this is not "real" inheritance, but as follows:
var car = new Car(2);
car instanceof Vehicle;
: instanceof . :
var car = new Car(2);
Vehicle.prototype.drive = function() {
console.log('vroom!');
};
car.drive();
, : Car Vehicle.
, .
, , , , Vehicle, , Car ( ).
, : 4 wheels Vehicle, . , , :
function Vehicle(wheels, color) {
this.wheels = wheels;
this.color = color;
}
:
function Car(doors, color) {
this.doors = doors;
Vehicle.call(this, 4, color);
}
Car.prototype = new Vehicle();
, Car Car Vehicle, Car. , color .
:
function Car(doors, color) {
this.doors = doors;
this.color = color;
}
Car.prototype = new Vehicle(4, null);
.