Browser javascript object library

I am currently working on creating a page that requires a Object.keys

method from Object class

. However, the browser to be launched is IGB 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 suggested solution was to find a specific Object class for javascript somewhere and reference it in my page ahead of time, like this:

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

      

However, I didn't find it in google search results.

I just figured it out, working on the project for too long, I only tried bits and pieces of it through the IGB, but I forgot to test this method and it became key to what I want to do. I would love to see this easily resolved with a simple link. It can be solved with other methods, but this would be the cleanest. Can anyone help me in the right direction?

+3


source to share


1 answer


You can try to execute 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;
    };
  }());
}

      

+4


source







All Articles