There is already numerous threads about prototypal inheritance in JavaScript, but this is not a copy due to laziness. I have literally read them all and have found almost as many different syntactic approaches as I have found answers, so it appears that I am not the only one that is confused on this subject!
Specifics:
My current approach 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);
Having tested the code I have found it works perfectly - as far as I can tell - but I have been told that this is not the best approach and that I should define the constructor like this:
Woman.prototype.constructor = Woman;
And not use the Person.call method in my actual constructor method. Two things, having been told that I can't see an easy way to then pass in the parameters using the 2nd approach, and also, just why? What I'm doing seems to work fine.
Am I missing something?
Is there some cases where what I'm doing will give unpredictable errors?
Can anyone give a definitive 'correct' approach and the reason for it?