Hacker News new | past | comments | ask | show | jobs | submit login

Is that the same as Subclass.prototype = new Superclass()?



no, Object.create() creates the object and sets __proto__ but it does not call the function. "new" also calls the function.


Thanks. Would an empty Superclass constructor result in the same thing?


That's correct. If the Superclass constructor had code in it, then that code would only be run once, regardless of the number of Subclass objects created. So, they would effectively share their super implementations, as if they were just one object. This doesn't matter when the super constructor doesn't do anything, since there is no difference.

The Object.create pattern avoids this by not calling the super constructor at all. If you need the super constructor to be called the way it is in classical inheritance, then you can call Superclass.call(this) in the Subclass constructor along with using Object.create. More details: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: