Created
April 8, 2020 18:55
-
-
Save nipundavid/1be8ef13563b92b9e775b7e3331af2d1 to your computer and use it in GitHub Desktop.
Prototype pattern implementation in 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
//Prototype Pattern | |
function car (manufacturer, engineType, transmission) { | |
this.manufacturer = manufacturer; | |
this.engineType = engineType; | |
this.transmission = transmission; | |
this.showManufacturer = function() { | |
console.log("Manufacturer is: "+this.manufacturer); | |
} | |
this.showEngineType = function() { | |
console.log("Engine type is: "+this.engineType); | |
} | |
this.showTransmission = function() { | |
console.log("Transmission is: "+this.transmission); | |
} | |
} | |
var myCar = new car("BMW", "Petrol", "Manual"); | |
myCar.showManufacturer(); | |
myCar.showEngineType(); | |
myCar.showTransmission(); | |
// added one variable in the class -> isHybrid | |
car.prototype.isHybrid = true; | |
// added one method in the class -> startCar | |
car.prototype.startCar = function () { | |
console.log("start my"+this.manufacturer); | |
} | |
var myAnotherCar = new car("Tesla", "Electric","automatic",true) | |
console.log(myAnotherCar.isHybrid); | |
myAnotherCar.startCar(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment