I'm Codeyear fellow and unfortunately prototype object concept is not explained.I google it and found tutorial. After learning,my understanding says that we use prototype object inheritance to save memory and share common properties between objects . am i right ? if yes , dont you think the below code is the bad practice. Since car constructor has already defined price,speed and & getPrice,why we need to define same thing again,since we are using the concept of inheritance . please explain . below is the code .
function Car( listedPrice ) {
var price = listedPrice;
this.speed = 0;
this.getPrice = function() {
return price;
};
}
Car.prototype.accelerate = function() {
this.speed += 10;
};
function ElectricCar( listedPrice ) {
var price = listedPrice;
this.speed = 0;
this.getPrice = function() {
return price;
};
}
ElectricCar.prototype = new Car(); // Please also explain why car constructor
// is not thowing error since we are not passing
// listedPrice parameter
myElectricCar = new ElectricCar(500);
console.log(myElectricCar instanceof Car);