Atli Þrastarson
Atli Þrastarson

Reputation: 55

javascript object library for browser

I am currently working on creating a page that requires the Object.keys method from the Object class. The browser that this needs to run in however is the IGB of Eve online. This browser does not support Object.keys and therefore

JavaScript error:

Uncaught TypeError: Object function Object() { [native code] } has no method 'keys'

My proposed solution of this was to find somewhere the defined Object class for javascript and reference it on my page beforehand like:

<script src="someURLtoJavascriptObjectClass"></script>

I have however not found it through my google searches.

I just realised this after having worked on the project for way too long, I only tried bits and pieces of it through the IGB but I forgot to test this method and it has become quite key to what I want to do. I would very much like this to resolve easily with a simple link. It can be solved with other methods but this would be by far the cleanest. Can someone help me in the right direction?

Upvotes: 3

Views: 101

Answers (1)

Oka
Oka

Reputation: 26355

You could try implementing the Polyfill in your code.

// 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;
    };
  }());
}

Upvotes: 4

Related Questions