Jaki jest prawidłowy wpływ prototypu w dziedziczeniu javascript?
Przeczytałem już kilka artykułów na temat dziedziczenia js (ten, ten, tenitp.)
WTen artykuł z Mozilli pokazane jest „klasyczne” dziedziczenie: (I ujednolicone przykłady)
// inherit Base
function Derived() { ... }
Derived.prototype = new Base(); <-------
Derived.prototype.constructor = Derived; <-------
Jednak wTen artykuł Widzę :
// inherit Base
function Derived() { ... }
Derived.prototype = Object.create(Base.prototype); <-------
Derived.prototype.constructor = Derived;
Ponadto widziałem to również:
Derived.prototype = Base.prototype;
Eksperymentowałem też i nie mogłem znaleźć zastosowaniaconstructor
afektacja:
Derived.prototype.constructor = Derived; <--- it still work if I skip this line
jeśli pominę tę linię,new Derived()
poprawnie dzwoniDerived()
tak czy inaczej.
Więc 1) co jest poprawne:
Derived.prototype = new Base();
Derived.prototype = Object.create(Base.prototype);
Derived.prototype = Base.prototype;
inny ?I 2) jestDerived.prototype.constructor = Derived;
naprawdę potrzebne? Czemu ?