First let see the example of how inheritance works in ES5.
var Person5 = function (name, yearOfBirth, job) {
this.name = name;
this.yearofBirth = yearOfBirth;
this.job = job;
}
Person5.prototype.calculateAge = function() {
var age = new Date().getFullYear() - this.yearOfBirth;
console.log(age);
}
var Athlete5 = function(name, yearOfBirth, job, olympicGames, medals) {
Person5.call(this, name, yearOfBirth, job);
this.olympicGames = olympicGames;
this.medals = medals;
}
Athlete5.prototype = Object.create(Person5.prototype); //will create new instance and point prototype of Person5 to Athlete5. So that now we can access the prototype of Person5 using object of Athlete5.
var johnAthlete5 = new Athlete5('lokman', 1990, 'engineer', 'swimmer', 3);
johnAthlete5.calculateAge();
No comments:
Post a Comment