Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare such Function if it does not exist [duplicate]

Tags:

javascript

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

I have a situation when some function X is being called. After some postbacks this function is no longer declared, but still being called by the code, obviously i get js error saying X is not defined . (call it a bug if you wish) but It is not under my control to not call it or to change the calling functionality.

What I would like to do is a fail safe that will declare such function if it does not exist. So the logic is:

If function not declared then declare one.

Is that possible in javascript i.e. to declare/register a function dynamically in global scope?

Thanks.

like image 726
jekcom Avatar asked Nov 28 '12 09:11

jekcom


2 Answers

if (typeof window.functionX === 'undefined') {
    window.functionX = function() {
      // fallback code here
    }
}
like image 186
acme Avatar answered Nov 20 '22 11:11

acme


Sure it is

if(!myFunc) {
  myFunc = function() {}
}
like image 4
Aesthete Avatar answered Nov 20 '22 13:11

Aesthete