1

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.

2 Answers 2

2

To share a method, assign it like this:

PlayerWhite.prototype.showName = function() {   alert(this.name); }
PlayerBlack.prototype.showName = PlayerWhite.prototype.showName;

To create shared parent:

Shared = function() { }

//here define shared methods
Shared.prototype.showName = function() { alert(this.name); }

PlayerWhite.prototype = new Shared();
PlayerBlack.prototype = new Shared();

//here define non-shared methods
PlayerWhite.prototype.showCounty = function() { alert(this.county);}
PlayerBlack.prototype.showState = function() { alert(this.state);}
Sign up to request clarification or add additional context in comments.

2 Comments

Excellent. This is exactly what I was looking for.
@NVG added correct way to share methods with a parent.
0

You are looking for simply inheriting methods. Please take a look at MDN for further explanation - it is really nicely explained out there.

https://developer.mozilla.org/pl/docs/Web/JavaScript/Inheritance_and_the_prototype_chain

Also - if you are looking forward to using nodeJs please take a look at it's "modules".

https://nodejs.org/api/modules.html

You can combine both methods ending up in something like this:

var BaseModel = require('PATHTOBASEMODEL/base_model');

ExtendedModel = Object.create(BaseModel);

ExtendedModel.prototype.yourFunction = function () { /* ... */ };

module.exports = ExtendedModel;

2 Comments

I appreciate your answer, but it seems easier to do it natively, as Radek Pech answered below.
Easier doesn't mean better. You have code hard to maintain using "native" way (you can't call it native though because both are) and you have code duplicates. If you want to inherit 50 methods you will write same code every time? The proper way actually is to declare basic abstract class Player and two players (or even N players) inheriting from that class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.