Using the prototype property

by Sukanya Halder

HTML

<!-- 
Notes : Using the prototype property
In JavaScript, everything, including the function, is an Object type, which has a prototype property. 
The prototype itself is an object containing properties and methods that should be available to all instances of the type you’re working with. However, this prototype is typically specified externally to the constructor function, so the prototype doesn’t have access to private variables. Therefore, you must expose the data for the prototype to work.
You might use the prototype property when creating functions that will be shared across all instances, but remember that the prototype is defined externally to the constructor function, so all properties must be public when using the this keyword.
-->

JavaScript

var car;
function Vehicle(year,modelname){    
   this.makemodel = modelname;
   this.makeyear = year;    
}
Vehicle.prototype.show = function(){
        alert(this.makemodel + "--"+this.makeyear);
    };
car = new Vehicle("BMW","2015");
car2 = new Vehicle("Mercedes","2015");

car2.show = function(){alert("hello");}
//console.log(car2.proto);
car.show();
car2.show();
//Vehicle("BMW","2015");