Skip to content

Instantly share code, notes, and snippets.

@jonfalcon
Created February 5, 2013 15:54
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jonfalcon/4715325 to your computer and use it in GitHub Desktop.
Javascript: Object keys polyfill
/**
* Polyfill for Object.keys
*
* @see: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/keys
*/
if (!Object.keys) {
Object.keys = (function () {
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
],
dontEnumsLength = dontEnums.length;
return function (obj) {
if (typeof obj !== 'object' && typeof obj !== 'function' || obj === null) throw new TypeError('Object.keys called on non-object');
var result = [];
for (var prop in obj) {
if (hasOwnProperty.call(obj, prop)) result.push(prop);
}
if (hasDontEnumBug) {
for (var i=0; i < dontEnumsLength; i++) {
if (hasOwnProperty.call(obj, dontEnums[i])) result.push(dontEnums[i]);
}
}
return result;
}
})()
};
@austinhinderer
Copy link

What license is this code available under? BSD? MIT? Apache?

@m93a
Copy link

m93a commented Feb 8, 2015

Copy link

ghost commented Jul 17, 2019

What license is this code available under? BSD? MIT? Apache?

One upvote for the question

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment