Search code examples
javajavascripthtmlgwtjsni

JSNI: Difference between JavaScriptObject and Element JSNI function parameter?


Using GWT JSNI what is the difference between JavaScriptObject and Element as parameter type for a JSNI method?

Consider the following method:

native void method(JavaScriptObject inputFile) /*-{

}-*/;

and this one:

native void method(Element inputFile) /*-{

}-*/;

Does it makes any different for the internal JSNI function if I use JavaScriptObject or Element?


Solution

  • An Element is a JavaScriptObject, but the converse is not true, not all java script objects are dom elements.

    From the perspective of Java, this is important - it makes sure that you don't pass something in which doesnt make sense, like a Window instance, or some json data, but only things which are really elements.

    From the perspective of JavaScript inside the JSNI method, it makes no difference, since JS doesn't understand types in those terms, and expects that whatever object is passed in has the right methods and fields on it.

    All other things being equal, be clearer - use Element when it makes sense, or at least add javadocs or name the parameter to be clear. Neither Java nor GWT nor Javascript are going to care if you specify JSO instead of Element, though if you accidentally pass in a non-Element and the method requires an element, you may get an error.