<!-- Notes : Best Way to implement a class
This class is wrapped in an IIFE. The wrapper encapsulates the function and the Vehicle
prototype. There is no attempt to make the data private. The code works as follows.
■■ When the code is loaded into the browser, the IIFE is immediately invoked.
■■ A nested function called Vehicle is defined in the IIFE.
■■ The Vehicle function’s prototype defines getInfo and startEngine functions that are on
every instance of Vehicle.
■■ A reference to the Vehicle function is returned, which is assigned to the Vehicle
variable.
This is a great way to create a class, and all future class examples use this pattern.
-->
JavaScript
var car;
var Vehicle = (function () {
function Vehicle(year, modelname) {
var model = modelname;
var modelmakeyear = year;
this.makemodel = function () {
return model;
};
this.makeyear = function () {
return modelmakeyear;
};
}
Vehicle.prototype.show = function () {
console.log(this.makemodel() + "--" + this.makeyear());
};
return Vehicle;
})();
car = new Vehicle("BMW", "2015");
car2 = new Vehicle("Mercedes", "2015");
car.show();
car2.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.