On Saturday, February 15, 2014 6:00:47 PM UTC+1, rjcarr wrote:
>
> 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 JavaScript
>
> Where 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
>     
> [email protected]::instanceFoo(Ljava/lang/String;)(s);
>
>     // Call instance method instanceFoo() on x
>     
> [email protected]::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?
>

The 'this' is the same here as it would be in Java, difference between 
'this' and 'x' is the same too.
 

>
> 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?
>

It has nothing to do with $entry() but the fact that you export a method of 
an object, and the way things (and specifically 'this') work in JavaScript.

Let's use pure JS:

function JSExample() { };
JSExample.prototype.instanceFoo = function(s) {
  // use s
};
JSExample.staticFoo = function(s) {
  // use s
};
JSExample.prototype.bar = function(x, s) {
  this.instanceFoo(s);
  x.instanceFoo(s);
  JSExample.staticFoo(s);
};

We can create a JSExample instance and call bar:

var y = new JSExample();
y.bar(new JSExample(), "msg");

Now export the method from 'y' without binding it to the JSExample instance 
and try to call it:

var foo = y.bar; // could be window.foo instead of var foo; y.bar is the 
same as JSExample.prototype.bar
foo(new JSExample(), "msg"); // fails because 'window' (the current 'this') 
does not have an 'instanceFoo' property
foo.call(y, new JSExample(), "msg"); // works because we explicitly set the 
'this' to 'y'

Now export the method, binding it to 'y' (using a closure) and call it:

var foo = function(x, s) { y.bar(x, s); };
foo(new JSExample(), "msg"); // works, because we call 'bar' on 'y'

Introduce $entry:

var foo = $entry(function(x, s) { y.bar(x, s); });
foo(new JSExample(), "msg"); // works the same

With ECMAScript 5, you could now use y.bar.bind(y) instead of using a 
closure, and with "use strict" the "default 'this'" would be 'null', not 
'window'.

When I say GWT doesn't free you from knowing JS, I really mean it!

-- 
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to