Javascript >> Add Methods After Inheritance

A constructor function that inherits its prototype object from a supertype constructor function can still have its own methods in addition to inherited methods.


For example, BlogRecord is a constructor that inherits its prototypefrom Record:



function Record() { }
Record.prototype.oldRecord= function() { console.log("old record"); };

function BlogRecord() { }

BlogRecord.prototype = Object.create(Record.prototype);
BlogRecord.prototype.constructor = BlogRecord;
BlogRecord.prototype.newRecord=function(){console.log("new record");}

let record= new BlogRecord();

record.oldRecord();
record.newRecord();

Comments

Popular posts from this blog

Tech Duos For Web Development

CIFAR-10 Dataset Classification Using Convolutional Neural Networks (CNNs) With PyTorch

Long-short-term-memory (LSTM) Word Prediction With PyTorch