Re: [jQuery] How do I check for the existence of an object?

2007-02-23 Thread Klaus Hartl
Daemach schrieb:
 I want to create a global object to store some settings in, but only if it
 doesn't exist already.  If it doesn't exist and I try to use something like
 if (myObj == 'undefined') myObj = new Object();  I get an error saying the
 object is not defined.
 
 Yeah I should know stuff like this ;)

You have to use the typeof operator:

if (typeof myObj == 'undefined') myObj = new Object();


But I would write that as follows anyway:

var myObj = myObj || {};



-- Klaus




___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] How do I check for the existence of an object?

2007-02-23 Thread Mike Alsup
 if (myObj == 'undefined') myObj = new Object();  I get an error saying the
 object is not defined.

Try this instead:

if (typeof myObj = 'undefined') myObj = {};

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] How do I check for the existence of an object?

2007-02-23 Thread Blair Mitchelmore
You can do (typeof myObj == 'undefined') and I think you can also do 
(myObj == undefined) thanks to the completely mind-blowing line of code 
at the beginning of jQuery
window.undefined = window.undefined;

-blair

Daemach wrote:
 I want to create a global object to store some settings in, but only if it
 doesn't exist already.  If it doesn't exist and I try to use something like
 if (myObj == 'undefined') myObj = new Object();  I get an error saying the
 object is not defined.

 Yeah I should know stuff like this ;)


___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] How do I check for the existence of an object?

2007-02-23 Thread Daemach

Thanks all!



Daemach wrote:
 
 I want to create a global object to store some settings in, but only if it
 doesn't exist already.  If it doesn't exist and I try to use something
 like if (myObj == 'undefined') myObj = new Object();  I get an error
 saying the object is not defined.
 
 Yeah I should know stuff like this ;)
 

-- 
View this message in context: 
http://www.nabble.com/How-do-I-check-for-the-existence-of-an-object--tf3280307.html#a9125240
Sent from the JQuery mailing list archive at Nabble.com.


___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/