Last active
April 29, 2017 15:17
-
-
Save eftakhairul/a153b2c1be2785dce5e51e5239761ed0 to your computer and use it in GitHub Desktop.
JS Inheritance Concept through prototype
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
/* | |
//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