Created
October 28, 2010 13:33
-
-
Save neall/651349 to your computer and use it in GitHub Desktop.
implement ES5-style Object.create
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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