Skip to content

Instantly share code, notes, and snippets.

@nipundavid
Created April 8, 2020 18:55
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 nipundavid/1be8ef13563b92b9e775b7e3331af2d1 to your computer and use it in GitHub Desktop.
Save nipundavid/1be8ef13563b92b9e775b7e3331af2d1 to your computer and use it in GitHub Desktop.
Prototype pattern implementation in JS
//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