Page 1 of 1

javascript "identity" of objects

Posted: Tue Jul 03, 2007 5:58 pm
by Luke
I have looked for this all over the place (including my ginormous javascript reference book). I can't find an operator or function/method that detects whether object a and object b live in the same place in memory... something like the is operator for python.

Posted: Tue Jul 03, 2007 6:05 pm
by superdezign
You mean if an object == the same object? Or if an object is a reference to another object? Or if an object is a copy of another object? Or if you have two pointers in different places to the same object?

Posted: Tue Jul 03, 2007 6:10 pm
by Luke
Or if you have two pointers in different places to the same object?
-- that one!

Posted: Tue Jul 03, 2007 6:17 pm
by superdezign
Sorry to say, but I don't think it's possible in the sense that you want. I've seen it done before by giving each instance of the object a unique id, but this required a round-about kind of constructor. It's a simple implementation, however.

Posted: Tue Jul 03, 2007 6:25 pm
by superdezign
Or not. Use the '===' operator.

Posted: Tue Jul 03, 2007 6:26 pm
by Luke
OK, maybe you can answer the larger question then. I'm trying to open a sub-menu when a user clicks a link. I want the sub-menu to close all other open submenus. When I loop through each of the other submenus and close them. it also closes the one the user clicked. So how do I detect in the loop which one is the one the user clicked?

Code: Select all

    		  $('.arrow').click(function(){
        		  
                      $('.arrow').each(function(){
            		  
                      if (this != the link the user clicked)
            	      {
                	     $(this).find('.close').click(); // close the submenu
            	      }
            		  
              });

Posted: Tue Jul 03, 2007 6:29 pm
by superdezign
Yeah, the '===' operator exists for that situation. It's called the 'identity operator.' Not sure how I forgot about it.

Posted: Tue Jul 03, 2007 6:34 pm
by Luke
hmm... I thought it just checked for same type + equality

Posted: Tue Jul 03, 2007 6:36 pm
by superdezign
Nope. '==' compares the attributes of objects, and '===' compares the identity (of which an object can only have 1).