Skip to content

Instantly share code, notes, and snippets.

@neall
Created October 28, 2010 13:33
Show Gist options
  • Save neall/651349 to your computer and use it in GitHub Desktop.
Save neall/651349 to your computer and use it in GitHub Desktop.
implement ES5-style Object.create
(function(){
if (typeof Object.create != 'function') {
Object.create = function(proto, propertiesObject) {
var pType = typeof propertiesObject;
if (pType == 'string' || pType == 'boolean' || pType == 'number') {
throw new TypeError('The second parameter to Object.create must be undefined or an object.');
}
var Constructor = function() {};
Constrcutor.prototype = proto;
var newObject = new Constructor();
if (propertiesObject) {
for (key in propertiesObject) {
if (propertiesObject.hasOwnProperty(key)) {
newObject[key] = propertiesObject[key];
}
}
}
return newObject;
};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment