In PHP/Java one can do:
class Sub extends Base
{
}
And automatically all public/protected methods, properties, fields, etc of the Super class become a part of the Sub class which can be overridden if necessary.
What’s the equivalent for that in Javascript?
I have changed how I do this now, I try to avoid using constructor functions and their
prototypeproperty, but my old answer from 2010 is still at the bottom. I now preferObject.create().Object.createis available in all modern browsers.I should note that
Object.createis usually much slower than usingnewwith a function constructor.jsfiddle
One of the big benefits of using Object.create is being able to pass in a defineProperties argument, which gives you significant control over how properties on the class can be accessed and enumerated over, and I also use functions to create instances, these serve as constructors in a way, as you can do initialization at the end instead of just returning the instance.
jsfiddle
This is my original answer from 2010: