JSFiddle - React, Tailwind, and code Playground
JavaScript
// This is the constructor of the parent class!
function List() {
this.Items = new Array();
}
// Add methods to the constructor, not to the instance ("this")
List.prototype.Add = function() { alert('please implement in object'); };
List.prototype.Count = function() { return this.Items.length; };
// Constructor of the child
function CDList() {
List.call(this); // <-- "super();" equivalent
}
// "extends" equivalent = Set up the prototype chain
var ctor = function() {};
ctor.prototype = List.prototype;
CDList.prototype = new ctor();
CDList.prototype.constructor = CDList;
// Overwrite actions
CDList.prototype.Add = function(Artist) {
this.Items.push(Artist);
};
//Create a new CDList object
var myDiscs = new CDList();
myDiscs.Add('Jackson');
alert(myDiscs.Count()); // expected: 1
//Create a second CDList object
var myDiscs2 = new CDList();
myDiscs2.Add('Walt');
myDiscs2.Add('Disney');
alert(myDiscs2.Count()); // expected: 2