Currently I implement inheritance in my JavaScript libraries as follows:
parent = function() { };
parent.prototype.hello = function() { alert('parent'); };
child = function() { };
child.prototype = parent.prototype;
However, I've noticed when I override a function in a child "class's" prototype, it undesirably overrides the parent's prototype as well:
child.prototype.hello = function() { alert('child'); }
(new parent()).hello(); // alerts "child" --> undesirable
(new child()).hello(); // alerts "child" --> desirable
And if I delete something from the child's prototype
delete child.prototype.hello;
then the parent's prototype is undesirably affected. So, maybe
child.prototype = parent.prototype;
is not the best way of implementing inheritance? Instead of making child "classes" reference the parent prototype, perhaps it would make more sense to copy the parent prototype to the child prototype?
_.extend(child.prototype, parent.prototype);