PDA

View Full Version : Simple question about checking if an object exists


krycek
11-04-2002, 09:37 PM
Ok, I was always under the impression that if you want to check if an object exists, you just say if (myObject) ... etc.

However that does not work for me right now! :confused:

My code is very simply:

if (collection) {
// do actions based on collection
}

collection in this case is an array which holds a list of the names of certain objects. This is set up in one script file, which may not always be present when the second script file, containing a checking function, is. The checking function should loop through the collection array, checking the objects.

However, if I do not include the file that sets up collection as an array, I get an error: collection is undefined. That is expected, so I inserted the code above, to check if collection exists first!

Same error! :eek:

So... anyone... what am I doing wrong...? I am sure it is simple but I can't see it! (I am sure things have always worked this way in the past!)

Cheers people!

::] krycek [::

Roy Sinclair
11-04-2002, 09:52 PM
if (typeof Collection == 'undefined') { ...

Edit: Fixed type (typeof, not typeOf - would hate to see stuff named consistently ;) )

krycek
11-04-2002, 10:13 PM
Thanks Roy - worked perfectly :) I had forgotten all about typeof :D silly me!

::] krycek [::