Skip to content

Instantly share code, notes, and snippets.

@Madhuka
Created November 16, 2013 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Madhuka/7497681 to your computer and use it in GitHub Desktop.
Save Madhuka/7497681 to your computer and use it in GitHub Desktop.
Constructor Pattern sample with JS
function Car(config) {
this.name = config.name;
this.engineSize = config.engineSize;
this.toString = function () {
return this.name + " has engine of " + this.engineSize + "cc";
};
this.start = function () {
return this.name + " start engine";
};
}
// create new instances of the car
var vitz = new Car({name:"Vitz", engineSize: 1000});
var prius = new Car( {name: "Prius", engineSize: 1800});
//now try our cars that is created
vitz.toString(); // --> Vitz has engine of 1000cc
prius.toString(); //--> Prius has engine of 1800cc
vitz.start(); //--> Vitz start engine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment