Room51 Technical Documentation

JavaScript Prototypes - Douglas Crockford's Beget Method

The beget() method

This method is used to create a new object with its prototype set to an object passed in.


if (typeof Object.beget !== 'function') {
	Object.beget = function(o) {
    	var F = function() {};
        F.prototype = o;
        return new F();
    };
}

We first check to see if there is already an Object.beget method; we don't want to overwrite an existing function (although any other type seems to be fair game).

A constructor, F, is defined, its prototype is set to the passed in object and then a new instance is returned.