News for hackers. Stuff that matters.
Re:The world's smallest and fastest classical JavaScript inh... by kybernetikos on Saturday February 15, 2014 @07:00AM (#7243533)
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.

Re:The world's smallest and fastest classical JavaScript inh... by camus2 on Saturday February 15, 2014 @10:13AM (#7243978)
nodejs has the util.inherits function which gives you an extra super_ shortcut to the superclass constructor,it makes sense to use it in node.
Re:The world's smallest and fastest classical JavaScript inh... by kybernetikos on Saturday February 15, 2014 @03:07PM (#7245105)
Quite 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
        }
      });
    };
Re:The world's smallest and fastest classical JavaScript inh... by jasonkostempski on Saturday February 15, 2014 @02:45PM (#7245029)
I've been using this pattern for years and never once ran into a problem. Does anyone here have any compelling reason not to?
Re:The world's smallest and fastest classical JavaScript inh... by kybernetikos on Saturday February 15, 2014 @02:59PM (#7245078)
I'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-javascrip...

Re:The world's smallest and fastest classical JavaScript inh... by Kiro on Saturday February 15, 2014 @07:46AM (#7243608)
Is that the same as Subclass.prototype = new Superclass()?
Re:The world's smallest and fastest classical JavaScript inh... by lucio on Saturday February 15, 2014 @07:48AM (#7243618)
no, Object.create() creates the object and sets __proto__ but it does not call the function. "new" also calls the function.
Re:The world's smallest and fastest classical JavaScript inh... by Kiro on Saturday February 15, 2014 @07:56AM (#7243626)
Thanks. Would an empty Superclass constructor result in the same thing?
Re:The world's smallest and fastest classical JavaScript inh... by shawnz on Saturday February 15, 2014 @08:52AM (#7243755)
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...