Skip to content

Instantly share code, notes, and snippets.

@eftakhairul
Last active April 29, 2017 15:17
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 eftakhairul/a153b2c1be2785dce5e51e5239761ed0 to your computer and use it in GitHub Desktop.
Save eftakhairul/a153b2c1be2785dce5e51e5239761ed0 to your computer and use it in GitHub Desktop.
JS Inheritance Concept through prototype
/*
//Literal way
var car = {
color:'black',
make: 'Audi',
model: 'A5',
move: function() {
return 'It is moving';
},
speakColor: function() {
return "Hey, my color is: " + this.color;
}
} */
//Parent
//Constructive way
function Car() {
this.color = 'black';
this.make = 'Audi';
this.model = 'A5';
this.move = function() {
return 'It is moving';
}
this.speakColor = function() {
return "Hey, my color is: " + this.color;
}
}
//Child
var Audi = function() {
this.faster = function() {
return 'I am really faster';
}
this.printModel = function() {
return "Model: " + this.model;
}
}
Audi.prototype = new Car(); //inheritance
var audi1 = new Audi(); //intance or creating real object of Audi which is child of Car
//console.log(audi1.color); //calling attribute
//console.log(audi1.move()); //calling method */
console.dir(audi1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment