Javascript – Checking the Existance of an Object

To test for an undefined function or variable, you must do so by referencing the parent object. This will usually be an Object that was instantiated previously or the current object ‘this’. If the variable was declared globally, you need to reference the top level object in the JavaScript hierarchy ‘window’.

For example:

var myVar = 5;

function SomeFunction() {
// blah, blah...
}

function SomeOtherFunction() {
//check for existance of global function SomeFunction()
if(window.SomeFunction) {
SomeFunction();
}

//check for existance of global variable myVar
if(window.myVar) {
myVar = 32;
}
}

Comments Off on Javascript – Checking the Existance of an Object

Categories javascript