1

The documentation of JSObject.equals says:

Determines if two JSObject objects refer to the same instance.

In contrast the following expression evaluates to false:

JSObject.getWindow(applet).equals(JSObject.getWindow(applet))

I have expected true...

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
snorbi
  • 2,590
  • 26
  • 36

1 Answers1

0

It appears that getWindow returns a new JSObject which describes the window. So each call to getWindow is a new instance, but with the same data, so equals returns false. Both JSObjects describe the window, but are not the same object.

public class MyClass
{
  int a;
  public MyClass(int arg)
  {
    a = arg;
  }

  public MyClass getMyClass()
  {
    return new MyClass(a);
  }

  public static void main(String args[])
  {
    MyClass parent = new MyClass(1);
    MyClass obj1 = parent.getMyClass();
    MyClass obj2 = parent.getMyClass();
    System.out.println(obj1.equals(obj2));        
  }
}

This prints false, because even though the objects have the same value in them, they are still different objects.

Edit: updated to use a get method to make it clearer.

Thomas
  • 5,074
  • 1
  • 16
  • 12
  • "Both JSObjects describe the window, but are not the same object" - You mean that JSObject.equals() does not work as described in the documentation, and there is no way to decide whether two JSObject instances refer to the same Javascript object? – snorbi May 01 '12 at 18:20
  • It works exactly as described. In this example, both obj1 and obj2 have the same data, '1'. Much like for you, the JSObject from both getWindow calls will have the same data, but are not the same object. You have two identical copies, but they are still two objects. `Refers to the same instance` returns true if they are the same object with a different name, but false if they are different objects with the same info. – Thomas May 01 '12 at 18:25
  • "This prints false" - It is because equals (and hashCode) is not overridden in the MyClass class. But based on the documentation, JSObject.equals() should work as I expected. Other parts of the documentation explicitly says: "You cannot use the == operator to compare two instances of JSObject. Use JSObject.equals." – snorbi May 01 '12 at 18:29
  • I appreciate your help and I completely understand your example code. But my problem relates very closely to JSObject, so what I would like to know: 1. Is JSObject.equals() really not working? (Or am I missing something important?) 2. Is there an alternative to check the equality of two Javascript references? – snorbi May 01 '12 at 18:32
  • So the ambiguity is on whether `instance` in the docs refers to an object instance or a window instance. In either case, you should be able to do a comparison, there are some ideas [here](http://www.codingforums.com/showthread.php?t=248977) – Thomas May 01 '12 at 19:45