javascript "identity" of objects

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

javascript "identity" of objects

Post 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.
Last edited by Luke on Tue Jul 03, 2007 6:10 pm, edited 1 time in total.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Or if you have two pointers in different places to the same object?
-- that one!
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Or not. Use the '===' operator.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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
            	      }
            		  
              });
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Yeah, the '===' operator exists for that situation. It's called the 'identity operator.' Not sure how I forgot about it.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

hmm... I thought it just checked for same type + equality
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Nope. '==' compares the attributes of objects, and '===' compares the identity (of which an object can only have 1).
Post Reply