Copy link to clipboard
Copied
I'm getting a "ReferenceError: Object.keys is not a function" error when trying to parse JSON from a jsx file:
Inside my jsx file I have:
$.evalFile("../json2.js");
try
{
var testDict = {"a": "1", "b": "2"};
var keys = Object.keys(testDict);
catch (e) {
alert("Caught error: " + e);
}
Does anyone have any idea why I'm getting this error when trying to parse the keys?
Thanks!
Sure it can't work... Object.keys is an EcmaScript 5 feature, and ExtendScript is stuck at version 3 (that's the why you're importing json2.js – no native json support in this dry land). According to Mozilla, this polyfill will work:
...// 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}).prop
Copy link to clipboard
Copied
Sure it can't work... Object.keys is an EcmaScript 5 feature, and ExtendScript is stuck at version 3 (that's the why you're importing json2.js – no native json support in this dry land). According to Mozilla, this polyfill will work:
// 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)) {
result.push(dontEnums);
}
}
}
return result;
};
}());
}
Hope this helps,
Davide Barranca
www.davidebarranca.com
Copy link to clipboard
Copied
Yes the polyfill works, thanks!