Created
November 16, 2013 08:38
-
-
Save Madhuka/7497681 to your computer and use it in GitHub Desktop.
Constructor Pattern sample with JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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