I’ve encountered an curious problem while trying to use some objects through JSNI in GWT. Let’s say we have javscript file with the function defined:
test.js:
function test(arg){
var type = typeof(arg);
if (arg instanceof Array)
alert('Array');
if (arg instanceof Object)
alert('Object');
if (arg instanceof String)
alert('String');
}
And the we want to call this function user JSNI:
public static native void testx()/ *-{
$wnd.test( new Array(1, 2, 3) );
$wnd.test( [ 1, 2, 3 ] );
$wnd.test( {val:1} );
$wnd.test( new String("Some text") );
}-*/;
The questions are:
- why
instanceofinstructions will always returnfalse? - why
typeofwill always return"object"? - how to pass these objects so that they were recognized properly?
instanceofshouldn’t be returning false all the time in your example unless you’re testing objects from a different window, because an array from one window is not an instance of theArrayconstructor of a different window.Using
instanceofis great when you need to test for a specific thing and you’re operating within one window (you do have to be aware of the string primitive vs. String object thing that scunliffe pointed out). Note that you need to be careful of your order, since an array is aninstanceofObject(as well asArray); this applies toStrings and all other objects as well.There’s an alternative that doesn’t have the window issue and which can readily be used for
switchstatements and the like if you’re doing dispatch:That looks odd, but what it does is use the
toStringfunction on theObjectprototype, which has a defined behavior (rather than using any override that the actual object you’re testing may have, which may have different behavior). So given this function:you’ll get these results:
and you’ll get those results regardless of what window the object you’re testing is coming from and regardless of whether you use a string primitive or a
Stringinstance.