How can I check if a function has been defined in JavaScript?

How can I check if a function has already been defined?

Will this work?

if(window.opener.MyFunctionBlah) {  ...  }

      

+1


a source to share


2 answers


if (typeof yourFunctionName === 'function') {
    yourFunctionName();
}

      



+6


a source


Yes. Functions are just properties of objects, like any other, so you can treat them as such. The conditional value you provided above will return true if this function (or any member window.opener

is called MyFunctionBlah

) is defined and is not zero.



+2


a source







All Articles