Quote Originally Posted by Jeff Mott View Post

I think Douglas Crockford was the first to come up with this workaround:

Code:
function object(o) {
    function F() {}
    F.prototype = o;
    return new F;
}
What this function does is return a new, empty object that inherits from the given object. So now our inheritance setup would look like this:

Code:
Son.prototype = object(Father.prototype);
Of course, there are still many ways to improve on this pattern. I like to re-package the object function into an "extend" method.

Code:
var BaseObj = {
    extend: function () {
        function F() {}
        F.prototype = this;
        return new F;
    }
};

var Father = BaseObj.extend();

var Son = Father.extend();
Ah, and this is actually a lot nicer than Crockford's naked object() function solution. So if I'm getting this correctly you should be able to just extend the Object object to give everything the extend method.

Code:
Object.prototype.extend = function() {
  function F() {}
  F.prototype = this;
  return new F;
}

father = {}
father.hello = function() {
  alert("Rudi");
}
father.hello();
==> Rudi

son = father.extend() 
son.hello()
==> Rudi
Now, rather than saying daimler = object(car), I can say daimler = car.extend() which is clearly much nicer.

Would there be a way to pass an object literal to the extend method and have all it's members added to the child object, or would that mean that this would always point to the original closure from within those methods?

One other thing that's confusing me. Why do I have to add the method to Object.prototype rather than simply Object? It's as though nothing inherits from Object, but everything inherits from Object.prototype. Does Object hang off the side of the inheritance tree, and if so, why?