Forum Discussion
Philip_Baird
10 years agoCommunity Expert
I have found it actually has nothing to do with the JSON parser, there is a problem with Arrays as I have bundled underscore.js in a Script Extension and the _.isArray() method is returning false negatives, i.e. it returns false if the object passed in is an Array, which is horribly wrong.
I have the following code in a Script Unit as well as in a Script Extension (originally from underscore.js):
function isArray( obj ) {
var ObjProto = Object.prototype;
var toString = ObjProto.toString;
Log.Message( "Type " + toString.call( obj ) );
return toString.call( obj ) === '[object Array]';
}
I then call this function from each source in another Script Unit as such:
function testArray() {
var arr = [ 123, 456 ];
Log.Message( SRC_TestArray.isArray( arr ) ); // Call Script Unit
Log.Message( Test.TestArray.isArray( arr ) ); // Call Script Extension
}
This provides the following output:
Type [object Array]
True
Type [object Object]
False
which proves that Arrays are somehow being cast to some sort of object.
This is a serious problem for me, my code behaves differently (in this case, completely incorrect) depending on where it is executed.
As a Developer, I need my code to execute consistently regardless of source so I can have confidence my test code is correct and not introducting potential errors.
At this point in time, I can have no confidence that passed in Arrays are treated correctly in Script Extensions.
Regards,
Phil Baird
Related Content
- 10 years agobfourie