Can simple javascript inheritance be simplified even further?

Posted by Will on Stack Overflow See other posts from Stack Overflow or by Will
Published on 2010-03-24T18:20:50Z Indexed on 2010/03/24 18:23 UTC
Read the original article Hit count: 379

John Resig (of jQuery fame) provides a concise and elegant way to allow simple JavaScript inheritance.

It was so short and sweet, in fact, that it inspired me to try and simplify it even further (see code below). I've modified his original function such that it still passes all his tests and has the potential advantage of:

  • readability (50% less code)
  • simplicity (you don't have to be a ninja to understand it)
  • performance (no extra wrappers around super/base method calls)
  • consistency with C#'s base keyword

Because this seems almost too good to be true, I want to make sure my logic doesn't have any fundamental flaws/holes/bugs, or if anyone has additional suggestions to improve or refute the code (perhaps even John Resig could chime in here!).

Does anyone see anything wrong with my approach (below) vs. John Resig's original approach?

if (!window.Class)
{
    window.Class = function() {};

    window.Class.extend = function(members)
    {
        var prototype = new this();

        for (var i in members)
            prototype[i] = members[i];

        prototype.base = this.prototype;

        function object()
        {
            if (object.caller == null && this.initialize)
                this.initialize.apply(this, arguments);
        }

        object.constructor = object;
        object.prototype = prototype;
        object.extend = arguments.callee;

        return object;
    };
}

And the tests (below) are nearly identical to the original ones except for the syntax around base/super method calls (for the reason enumerated above):

var Person = Class.extend(
{
    initialize: function(isDancing)
    {
        this.dancing = isDancing;
    },
    dance: function()
    {
        return this.dancing;
    }
});

var Ninja = Person.extend(
{
    initialize: function()
    {
        this.base.initialize(false);
    },
    dance: function()
    {
        return this.base.dance();
    },
    swingSword: function()
    {
        return true;
    }
});

var p = new Person(true);
alert("true? " + p.dance()); // => true

var n = new Ninja();
alert("false? " + n.dance()); // => false
alert("true? " + n.swingSword()); // => true
alert("true? " + (p instanceof Person && p instanceof Class && n instanceof Ninja && n instanceof Person && n instanceof Class));

© Stack Overflow or respective owner

Related posts about simple

Related posts about JavaScript