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

47 views
Skip to first unread message

doles

unread,
Aug 30, 2011, 11:48:42 AM8/30/11
to google-we...@googlegroups.com
Sorry if the question was confusing. I am writing an JS overlay type library to our pre-existing javascript and have this question. I have a "public static native MyJSObject create()" method in my class that extends JavaScriptObject. All works great. My question is what should i be really returning in the create method? Currently, i return "this" in the js object and it works well. Earlier, I had an implementation to create a new object within my create method and then returning that and maintaining that. so, the create method would have a body like: this.myjso={};return myjso; and then all subsequent instance operations would operate on this.myjso instead of myjso.

Is there a best practice or its all the same? Thank you!!!

doles

unread,
Aug 30, 2011, 11:49:59 AM8/30/11
to google-we...@googlegroups.com
another related question: is the "this" inside a JSO always a brand new object or does GWT re-use an instance whose lifecycle it tries to maintain?

Thomas Broyer

unread,
Aug 30, 2011, 12:31:10 PM8/30/11
to google-we...@googlegroups.com
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!

Sachin Dole

unread,
Aug 30, 2011, 12:54:50 PM8/30/11
to google-we...@googlegroups.com
thank you for the under-the-covers explanation of everything that i just experienced from-the-outside over the last few hours! :-) Seriously.

--
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-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

Reply all
Reply to author
Forward
0 new messages