While there are numerous discussions on prototypal inheritance in JavaScript already, I assure you this is not just another lazy copy. Having thoroughly read all existing threads, I've noticed a plethora of syntactic approaches and varying answers which have left me as bewildered as others on this subject!
Here's the gist of my current approach:
My implementation currently looks like this.
var Person = function(name) {
this.name = name;
this.greeting = function() {
alert("Greetings, I am " + name);
}
}
var bob = new Person("bob");
bob.greeting();
var Woman = function(name) {
Person.call(this, name);
this.gender = "female";
}
Woman.prototype = Object.create(Person.prototype);
var brenda = new Woman("brenda");
brenda.greeting();
Person.prototype.eats = true;
alert(brenda.eats);
Although everything seems to be working smoothly so far, I've been advised that it might not be the optimal approach. Specifically, I was suggested to redefine the constructor like so:
Woman.prototype.constructor = Woman;
The suggestion also entails discarding the use of Person.call method within the actual constructor. However, upon considering this alternative, I find myself at a loss when it comes to passing parameters using this new approach. Plus, the question lingers - why should I make the change when what I'm doing is effective?
Am I overlooking something crucial?
Could there be scenarios where my current implementation might lead to unforeseen errors?
Can anyone offer a definitive 'correct' way forward along with the rationale behind it?