25

How do I see if a certain object has been loaded, and if not, how can it be loaded, like the following?

if (!isObjectLoaded(someVar)) {
    someVar= loadObject();
}

10 Answers 10

37

If it is an object then you should just be able to check to see if it is null or undefined and then load it if it is.

if (myObject === null || myObject === undefined) {
   myObject = loadObject();
}

Using the typeof function is also an option as it returns the type of the object provided. However, it will return null or undefined if the object has not been loaded so it might boil down a bit to personal preference in regards to readability.

Sign up to request clarification or add additional context in comments.

3 Comments

Isn't a test like if (myObject == null) (or undefined) enough, since that's precisely the point of the simple equality? Or I am missing something?
@PhiLho - Depending upon the object type, the object could be null, or it could be undefined. JavaScript treats them as two different things.
@Rob @PhiLho This: x == null is equivalent to this: x === null || x === undefined (under the condition that the global variable undefined hasn't been modified). Therefore, instead of your expression you can just write myObject == null. It's not only smaller, but also more robust since it's indifferent to assignments to the variable undefined.
27
if(typeof(o) != 'object') o = loadObject();

1 Comment

Remember that typeof returns "object" for nulls, so you'll probably want something like [if(null !== x && 'object' == typeof(x)){]
5
myObject = myObject || loadObject();

Comments

3

I'm not sure what you mean by "loaded"... does the variable object exist and simply doesn't have the type you want? In that case, you'll want something like:

function isObjectType(obj, type) {
    return !!(obj && type && type.prototype && obj.constructor == type.prototype.constructor);
}

and then use if (isObjectType(object, MyType)) { object = loadObject(); }.

If object is not populated with anything before your test (ie - typeof object === 'undefined') then you just need:

if ('undefined' === typeof object) { object = loadObject(); }

Comments

3

You probably want to see if a given object is defined

Especially if its done in an asynchronous thread with a setTimeout to check when it turns up.

  var generate = function()
  { 
      window.foo = {}; 
  }; 
  var i = 0;
  var detect = function()
  {
     if( typeof window.foo == "undefined" ) 
     {
           alert( "Created!"); 
           clearInterval( i );
     }
   };
   setTimeout( generate, 15000 ); 
   i = setInterval( detect, 100 ); 

should in theory detect when window.foo comes into existance.

Comments

3

If by loaded you mean defined, you can check the type of the variable with the typeof function. HOWEVER typeof has a few quirks, and will identify an Object, an Array, and a null as an object

alert(typeof(null));

Identifying a null as a defined object would probably cause your program to fail, so check with something like

if(null !== x && 'object' == typeof(x)){
    alert("Hey, It's an object or an array; good enough!");
}

Comments

2

You can also just use a shortcut if(obj)

Comments

2

If you want to detect a custom object:

// craete a custom object
function MyObject(){

}

// check if it's the right kind of object
if(!(object instanceof MyObject)){
   object = new MyObject();
}

Comments

1

typeof(obj) would return "object" for an object of a class among other possible values.

Comments

1
if (!("someVar" in window)) {
  someVar = loadObject();
}

will tell you whether any JS has previously assigned to the global someVar or declared a top-level var someVar.

That will work even if the loaded value is undefined.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.