If you attach all the properties to the prototype (which is preferable, at least for the methods),
function Foo() {} Foo.prototype.foo1 = null; Foo.prototype.foo2 = function() { return false;};
then assigning a parent prototype to a child prototype is enough:
function inherit(Child, Parent) { var Tmp = function(){}; Tmp.prototype = Parent.prototype; Child.prototype = new Tmp(); Child.prototype.constructor = Child; } inherit(Bar, Foo);
Here we used an intermediate design function to decouple the two prototypes. Otherwise, if you change it, you will change the other as well (since they refer to the same object). This method is actually quite popular and is used by several libraries.
If not, you should call the parent constructor function inside the constructor function of the children:
function Bar() { Foo.call(this); }
This is what you should always do to assign the properties set in the constructor function to the current object.
One more note to your path:
Bar.prototype = new Foo();
this should work (also in IE actually), but has two main drawbacks:
source share