Declare such a function if it does not exist

Possible duplicate:
if function does not exist write function - javascript

I have a situation where some function X is called. After some postbacks, this function is no longer declared, but still called by the code, obviously, I get a js error saying X is not defined . (call it a mistake if you want), but Do not call, not under my control, but change the functionality of the call.

What I would like to do is fault tolerance, which will declare such a function if it does not exist. So the logic is this:

If the function is not declared, declare it.

Is it possible in javascript to declare / register a function dynamically in a global scope?

Thanks.

+6
source share
2 answers
 if (typeof window.functionX === 'undefined') { window.functionX = function() { // fallback code here } } 
+7
source

Of course it is

 if(!myFunc) { myFunc = function() {} } 
+2
source

All Articles