public class Main implements EntryPoint {
public void onModuleLoad() {
export();
}
public void log(String msg) {
...
}
public static native void export() /*-{
$wnd.log = $entry(this.@package.Main::log(Ljava/lang/String));
}-*/;
public class Main implements EntryPoint {
public void onModuleLoad() {
export();
}
public static void log(String msg) {
...
}
public static native void export() /*-{
$wnd.log = $entry(@package.Main::log(Ljava/lang/String));
}-*/;
I should point out the differences. In the first non-working example I use 'this' in my export. In the second example I remove the 'this' and declare my log() method as static.
public class Main implements EntryPoint {
public void onModuleLoad() {
this.export();
}
public void log(String msg) {
...
}
public native void export() /*-{
$wnd.log = $entry(this.@package.Main::log(Ljava/lang/String));
}-*/;
public class Main implements EntryPoint {
public void onModuleLoad() {
Main.export(this);
}
public void log(String msg) {
...
}
public static native void export(Main main) /*-{
$wnd.log = $entry(main.@package.Main::log(Ljava/lang/String));
}-*/;
public class Main implements EntryPoint {
public void onModuleLoad() {
Main.export();
}
public static void log(String msg) {
...
}
public static native void export() /*-{
public class JSNIExample {
String myInstanceField;
static int myStaticField;
void instanceFoo(String s) {
// use s
}
static void staticFoo(String s) {
// use s
}
public native void bar(JSNIExample x, String s) /*-{
// Call instance method instanceFoo() on this
this.@com.google.gwt.examples.JSNIExample::instanceFoo(Ljava/lang/String;)(s);
// Call instance method instanceFoo() on x
x.@com.google.gwt.examples.JSNIExample::instanceFoo(Ljava/lang/String;)(s);
// Call static method staticFoo()
@com.google.gwt.examples.JSNIExample::staticFoo(Ljava/lang/String;)(s);Thanks for the quick responses. That makes sense that since the jsni method is declared static that you can't use this, but I'm not following Thomas's explanation. Maybe I could just get an explanation from the docs example here:
http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html#methods-fields
Under the example: Accessing Java fields from JavaScriptWhere it has:public class JSNIExample { String myInstanceField; static int myStaticField; void instanceFoo(String s) { // use s } static void staticFoo(String s) { // use s } public native void bar(JSNIExample x, String s) /*-{ // Call instance method instanceFoo() on this this.@com.google.gwt.examples.JSNIExample::instanceFoo(Ljava/lang/String;)(s); // Call instance method instanceFoo() on x x.@com.google.gwt.examples.JSNIExample::instanceFoo(Ljava/lang/String;)(s); // Call static method staticFoo() @com.google.gwt.examples.JSNIExample::staticFoo(Ljava/lang/String;)(s);What's the difference here between using the 'this' and the passed in 'x'? What does 'this' represent in this example?
As for Thomas's explanation, are you saying that by calling $entry it creates a new inner function (or closure) where 'this' is no longer what it was? So if I didn't use $entry then 'this' would be what I expect it is?
--
Thanks for the explanation and examples. I actually know javascript quite well, including the ramifications of using this inside of closures, but as I said, I didn't know that $entry was setting up a closure.