0

I have been reading some articles about javascript inheritance and how to mimic the behavior of classical languages in regards to subclassing.

However, it does not seem quite right to me the way the prototype property is being initialized in the subclass.

/* Super class */
function Mammal(){}

Mammal.prototype.breathe = function(){}

/* Sub class */
function Cat(){}

/* Is this really necessary? */
Cat.prototype = new Mammal()

I wonder would not it be enough to set it like this:

/* Would not this accomplish the expected result? */
Cat.prototype = Mammal.prototype;

By using the first assignment we are also bringing along any external properties from the superclass which is not the purpose of the prototype property, I guess.

utxeee
  • 953
  • 1
  • 12
  • 24
  • 2
    FWIW, I [added an answer there](http://stackoverflow.com/a/39933612/157247) which is a bit more up to date than the previous ones, and a bit cleaner (but then, I *would* think that). – T.J. Crowder Oct 08 '16 at 14:34
  • 1
    FWIW, both approaches in the question are wrong. Have a look at http://stackoverflow.com/a/17393153/1048572?Benefits-of-using-Object.create-for-inheritance – Bergi Oct 08 '16 at 16:59

0 Answers0