Prototypal Inheritance
Sat Nov 07 2020 13:14:47 GMT+0000 (Coordinated Universal Time)
Saved by
@snigdhakhurana10
//Function Constructor : Person has an instance property : name
function Person(name){
this.name = name;
}
//Person Instances
let john = new Person('John');
let sara = new Person('Sara');
console.log(john.name); //John
console.log(sara.name); //Sara
Person.prototype.walk = function(){
console.log(this.name + ' is walking.');
}
john.walk(); //John is walking
sara.walk(); //Sara is walking
//Programmer Constructor : Wants to inherit Person
function Programmer(name, programmingLanguage){
Person.call(this, name);
this.programmingLanguage = programmingLanguage || '';
}
Programmer.prototype = Object.create(Person.prototype);
Programmer.prototype.constructor = Programmer;
var snig = new Programmer('Snigdha', 'Javascript');
console.log(snig.name);
console.log(snig.programmingLanguage);
snig.walk();
content_copyCOPY
Comments