I am rather new to Javascript's way of dealing with constructors,methods and prototypes.
I want to create two constructors that have many different custom methods, but also have some methods in common. Currently I do things like this:
function PlayerWhite(n,s) { this.name = n; this.state = s;}
function PlayerBlack(n,c) { this.name = n; this.county = c; }
PlayerWhite.prototype.showCounty = function() { alert(this.county);}
PlayerBlack.prototype.showState = function() { alert(this.state);}
PlayerWhite.prototype.showName = function() { alert(this.name); }
PlayerBlack.prototype.showName = function() { alert(this.name); }
So the contents of the "showName" method is identical for both constructors. The code for "showName" may change and it will be the same for both, so I dont want to do double edits each time I will do an update to the showName method.
Of course, I could use just 1 constructor (function Player), call it twice to build each of the two objects, then assign the common methods to each object and then apply the distinct methods to each object using prototype , but what if I already wrote hundreds of lines of code and I have many objects created from the PlayerBlack and PlayerWhite constructors and I just want to add a new method that could be used between all the existing objects created through PlayerBlack or PlayerWhite?
I tried something like this, but it doesnt work:
PlayerWhite.prototype.showName,
PlayerBlack.prototype.showName = function() { alert(this.name); }
I am looking for a solution that would work in nodeJS.