<!--
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");
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.