Created
July 21, 2015 15:47
-
-
Save lski/57e370adeaea8356f622 to your computer and use it in GitHub Desktop.
Basic Object.keys polyfill
This file contains 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 (!Object.keys) { | |
Object.keys = function (o) { | |
if (o !== Object(o)) { | |
throw new TypeError('Object.keys called on a non-object'); | |
} | |
var k = [], | |
p; | |
for (p in o) { | |
// Use direct access, just in case this object has overridden the hasOwnProperty method which does something different. | |
// Also as it wont go through the prototype chain to find the method, it should be quicker | |
// Finally hasOwnProperty is set on the lowest level prototype 'Object' which all object derive from. However Object.create(null) can be used to | |
// create an object that has no base prototype, meaning hasOwnProperty would not exist (however this usage of Object.create is not recommended) | |
if (Object.prototype.hasOwnProperty.call(o, p)) { | |
k.push(p); | |
} | |
} | |
return k; | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment