I know that the following is some kind of the same in GWT:
Window and $wnd
Document and $doc
Are there any differences, beside that the first is used in Java and the second in JSNI (JavaScript)?
I know that the following is some kind of the same in GWT:
Window and $wnd
Document and $doc
Are there any differences, beside that the first is used in Java and the second in JSNI (JavaScript)?
Share Improve this question asked Oct 19, 2013 at 17:31 user2892258user28922581 Answer
Reset to default 16Window
is a class plenty of static methods, which you can use for getting or setting certain properties of the browser window, but actually it is not the native browser window object. These method implementations are eventually JSNI
blocks which use the $wnd
variable to set/get those properties. Like Window.alert()
whose implementation would be $wnd.alert()
. Note that Window.java
doesn't have access for everything in the browser's window object like window.console
etc.
GWT uses $wnd
instead of window
because piled code is executed normally in an iframe, and in this context, window
will reference the iframe window while $wnd
will reference the parent window. The same occurs with $doc
which is a reference in the iframe to the parent document.
In the other side Document
is a java class which extends JavaScriptObject
, it means that it is an Overlay type which basically means that it is a special wrapper for a native javascript object, it does not modify the underlying JavaScript but adds a set of java methods to interact with it. It can be safely cast when passing to jsni.
In summary, although Document
and $doc
are not the same in java world, when it is piled they will be the same, otherwise Window
it's not an overlay of $wnd
, it is just a way to access certain methods of the browser window.
Although GWT piled code delegates to native js objects and methods, don't try to find similarities between js and java objects. GWT has designed an API to develop ajax applications using a set of java objects, widgets, patterns, etc. Some objects and methods are named in the same way, but almost the API is different. There are though, other projects which piles java to javascript which has a rigid parallelism between both worlds like ST-JS, and GWT provides an experimental library called Elemental whose API is almost identical to javascript (it's only available for Chrome).