<!--
Notes :
There is no class keyword in JavaScript, but you can simulate a class by starting with a function, which is actually the constructor function of the object
There are several problems with this code. All the variables are defined without the var keyword, so year, make, model, and getInfo are automatically defined in the global scope and are accessible from anywhere.
The Vehicle function accepts three parameters and doesn’t return anything. Instead, it is setting global variables, and there is no provision for multiple instances.
The problem is that year, make, and model of the second
vehicle replaced year, make, and model of the first vehicle.
there is no encapsulation
-->
JavaScript
var car;
function Vehicle(year,modelname){
makemodel = modelname;
makeyear = year;
show= function(){
alert(makemodel + "--"+makeyear);
}
}
car = Vehicle("BMW","2015");
car2 = Vehicle("Mercedes","2015");
//car2.show = function(){alert("hello");}
//car.show();
//car2.show();
//Vehicle("BMW","2015");
show();
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.