Tuesday, August 30, 2011

Re: What is the relation of "this"'s lifecycle to a javascript object lifecycle?

thank you for the under-the-covers explanation of everything that i just experienced from-the-outside over the last few hours! :-) Seriously.

On Tue, Aug 30, 2011 at 11:31 AM, Thomas Broyer <t.broyer@gmail.com> wrote:
Huh!

A JSO is only a "view" from the Java world on an object living in the JS world.

A JSO class won't compile like other classes, it'll be totally removed; only its methods will be kept, changed in to static ones (i.e. "public native final setFoo(String foo) /*-{ this.foo = foo; }-*/;" will ultimately be turned into something like "public static native final setFoo(JavaScriptObject jso, String foo) /*-{ jso.foo = foo; }-*/;", which will probably be inlined in the end, for such a simple method).

So, the JSO and the underlying JS object are exactly the same (there's some trickery to make it look like so in DevMode, despite the ping-pong between the browser's JS engine and the DevMode, through the devmode plugin in the browser and communications through the network).

That also means that within a static method, there's no "this". It doesn't throw because there's always a "this" in JS, but it's a mistake to use it here (it's surprising it didn't fail for you at runtime, with really weird errors).

If you make a create() method, then you should create an object in here, either implement it in Java using "return JavaScriptObject.createObject().cast()", or in JSNI using a simple "return {}"; or something more complex if you're wrapping a JS library (e.g. "return new $wnd.MyJsClass()"), but do not, by any mean, use "this" in a static method!

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/R6MoTkTc6cAJ.

To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment