How to check if a function is already defined ?
function test(){}
if(typeof test != "undefined")
// function is allready defined
typeof return string,so you can use the JavaScript typeof operator to find the type of a JavaScript variable.
if ( typeof(youerFunctionName) === 'undefined' )
{
console.log('undefined');
}
Like so:
if (typeof myFunc != 'undefined') {
// Assign myFunc
}
Don't just test it against undefined
, which is not a constant and can be reassigned.
Say your function is called func
:
if (func) {
// Function is already defined (or at least something is defined there)
} else {
// Function is not defined
}