I read several articles on js inheritance already (this one, this one, this one, etc.)
In this article from Mozilla, "classic" inheritance is shown as this : (I uniformized examples)
// inherit Base
function Derived() { ... }
Derived.prototype = new Base(); <-------
Derived.prototype.constructor = Derived; <-------
However in this article I see :
// inherit Base
function Derived() { ... }
Derived.prototype = Object.create(Base.prototype); <-------
Derived.prototype.constructor = Derived;
Moreover I have also seen this :
Derived.prototype = Base.prototype;
And I also experimented and couldn't find the use of constructor affectation :
Derived.prototype.constructor = Derived; <--- it still work if I skip this line
if I skip this line, new Derived() correctly calls Derived() anyway.
So 1) what is correct :
Derived.prototype = new Base();Derived.prototype = Object.create(Base.prototype);Derived.prototype = Base.prototype;- other ?
And 2) is Derived.prototype.constructor = Derived; really needed ? Why ?
Object.create()is a relatively recent command which did not exist previously. See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… (the browser compatibility overview is at the bottom of that page). If you use only browsers that have it, use it, otherwise don't - how's that for a ridiculously obvious piece of advice? :)