4 ms·
Weird. As far as I'm concerned, the smallest classical inheritance pattern in javascript is Subclass.prototype = Object.create(Superclass.prototype); And
by kybernetikos 13y ago
Weird. As far as I'm concerned, the smallest classical inheritance pattern in javascript is
Subclass.prototype = Object.create(Superclass.prototype);
And combined with calling your superclasses constructor in your own constructor, that's it. Two lines at most.
You can get a little more complex if you want. In topiarist, I also copy all the 'static' properties from the superconstructor onto the child constructor, check that the prototype hasn't already been modified before replacing it so as to fail fast, and set up the constructor field. But all this stuff is just icing. The smallest classical inheritance pattern is just what I wrote above, and I tend to think that all these articles that make it sound like some deep magic is going on are doing more of a disservice to javascript developers than a service.
- Kiro 13y agoIs that the same as Subclass.prototype = new Superclass()?
- lucio 13y agono, Object.create() creates the object and sets __proto__ but it does not call the function. "new" also calls the function.
- Kiro 13y agoThanks. Would an empty Superclass constructor result in the same thing?
- shawnz 13y agoThat'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/Reference/Global_Objects/Object/create#Classical_inheritance_with_Object.create https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- deleted 13y ago[deleted]
- camus2 13y agonodejs has the util.inherits function which gives you an extra super_ shortcut to the superclass constructor,it makes sense to use it in node.
- kybernetikos 13y agoQuite right, if you're in node, you may as well use util.inherits, especially since it basically does exactly the same thing (plus super_ and constructor): exports.inherits = function(ctor, superCtor) { ctor.super_ = superCtor; ctor.prototype = Object.create(superCtor.prototype, { constructor: { value: ctor, enumerable: false, writable: true, configurable: true } }); };
- jasonkostempski 13y agoI've been using this pattern for years and never once ran into a problem. Does anyone here have any compelling reason not to?
- kybernetikos 13y agoI've been doing something like this since 2007 on code bases that run into the many hundreds of thousands of lines of code. It is actually the correct way to do classical inheritance in javascript, everything else is trimming, convenience, backwards compatibility or is simply wrong. In fact almost every other 'pattern' out there does this at the heart of it. So, for example you'll see some people use a surrogate instead of doing Object.create (as I did when I first started doing this). That's just doing exactly the same thing as Object.create, but in such a way as to work in pre ecmascript 5 browsers - we didn't have Object.create back then. http://kybernetikos.com/2007/02/07/inheritance-and-javascript/ http://kybernetikos.com/2007/02/07/inheritance-and-javascrip...