Skip to content

Instantly share code, notes, and snippets.

@jfromaniello
Created May 17, 2015 23:31
Show Gist options
  • Select an option

  • Save jfromaniello/0d02dc4b9138717621e8 to your computer and use it in GitHub Desktop.

Select an option

Save jfromaniello/0d02dc4b9138717621e8 to your computer and use it in GitHub Desktop.

Revisions

  1. jfromaniello created this gist May 17, 2015.
    41 changes: 41 additions & 0 deletions mdn-object-keys.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
    if (!Object.keys) {
    Object.keys = (function() {
    'use strict';
    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 = [], prop, i;

    for (prop in obj) {
    if (hasOwnProperty.call(obj, prop)) {
    result.push(prop);
    }
    }

    if (hasDontEnumBug) {
    for (i = 0; i < dontEnumsLength; i++) {
    if (hasOwnProperty.call(obj, dontEnums[i])) {
    result.push(dontEnums[i]);
    }
    }
    }
    return result;
    };
    }());
    }