<!--
Notes :
JavaScript has an Object type, and you can use it to create an object programmatically.
Object has a prototype object that is cloned when you use the new keyword to create a new Object instance.
The prototype object has the
following inherited methods.
■■ constructor The function that is called to initialize a new object
■■ hasOwnProperty Returns a Boolean indicator of whether the current object has the
specified property
■■ isPrototypeOf Returns a Boolean indicator of whether the current object is in the specified object’s prototype object chain
■■ propertyIsEnumerable Returns true if the object can be enumerated in a for...in
loop
■■ toLocalString Converts a date to a string value based on the current local
■■ toString Returns the string representation of the current object
■■ valueOf Returns the value of the current object converted to its most meaningful primitive value
Although the getVehicle function encapsulates the object creation, the properties are all public.
you might encounter the problem that every vehicle’s type is Object, and you might want to create a Vehicle class to have a named Vehicle type.
-->
JavaScript
var car;
var getVehicle= function(makeyear,modelname){
var vehicle = new Object();
vehicle.makemodel = modelname;
vehicle.makeyear = makeyear;
vehicle.show= function(){
alert(this.makemodel + "--"+this.makeyear);
}
return vehicle;
}
car = getVehicle("BMW","2015");
car2 = getVehicle("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.