Qual é a afetação de protótipo correta em herança de javascript?
Eu já li vários artigos sobre herança js (este, este, esteetc.)
EmEste artigo do Mozilla, herança "clássica" é mostrada como esta: (eu exemplos uniformizados)
// inherit Base
function Derived() { ... }
Derived.prototype = new Base(); <-------
Derived.prototype.constructor = Derived; <-------
No entanto,Este artigo Entendo :
// inherit Base
function Derived() { ... }
Derived.prototype = Object.create(Base.prototype); <-------
Derived.prototype.constructor = Derived;
Além disso eu também vi isto:
Derived.prototype = Base.prototype;
E eu também experimentei e não consegui encontrar o uso deconstructor
afetação:
Derived.prototype.constructor = Derived; <--- it still work if I skip this line
se eu pular essa linha,new Derived()
chama corretamenteDerived()
de qualquer forma.
Então 1) o que está correto:
Derived.prototype = new Base();
Derived.prototype = Object.create(Base.prototype);
Derived.prototype = Base.prototype;
de outros ?E 2) éDerived.prototype.constructor = Derived;
realmente necessário? Por quê ?