JSFiddle - React, Tailwind, and code Playground
JavaScript
// class person
function Person(name) {
this.name = name;
}
Person.prototype.getName = function(){
alert('Person getName called for ' + this.name);
return this.name;
}
// original way
var reader = new Person('John Smith');
reader.getName = function() {
Person.prototype.getName.apply(this, arguments);
return('yossi balan');
}
reader.getName();
// Alternative way, setting up an actual Reader class
// that can also be inherited from later.
function Reader(name) {
// Calls the person constructor with this as its object
Person.call(this, name);
}
// Make our prototype from Person.prototype
Reader.prototype = Object.create(Person.prototype);
Reader.prototype.getName = function() {
alert('READER getName called for ' + this.name);
Person.prototype.getName.apply(this, arguments);
return 'Something';
}
Reader.prototype.constructor = Reader;
var reader = new Reader("Some Reader");
reader.getName();
// Now if you want to extend the chain further and inherit from Reader
function VoraciousReader(name) {
Reader.call(this, name);
}
// Now define VoraciousReader's methods on VoraciousReader.prototype
VoraciousReader.prototype = Object.create(Reader.prototype);
VoraciousReader.prototype.constructor = VoraciousReader;
//VoraciousReader.prototype.someMethod = ... etc.
var v = new VoraciousReader('Voracious Reader');
v.getName();