Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I conditionally define a function in javascript?

Tags:

javascript

Consider the following script:

if (false)​ {
    function foo() {
        alert("FOO");
    }
}
foo(); // alerts "FOO"

As you can see, the function foo is defined and works even though the condition for the if fails.

The reason I want to do this is that I am developing an extension for InDesign and I made a script called utilities.jsx which holds various functions which are used in other scripts. In all the scripts that need these functions, I simply use:

app.doScript(File(scriptsFile.parent.fsName + "/t_utilities.jsx"), ScriptLanguage.JAVASCRIPT);

which defines all the functions I need and then I can use them. Now, since this script will be called multiple times by various files in an unpredictable order (the user decides), I thought I might only define the functions if they haven't been defined before (if utilities.jsx hasn't run yet). Kind of like require_once in php.

I was thinking of something like the following, but that doesn't work as the functions are defined every time the script is run:`

var utilitiesHasRun;
if (utilitiesHasRun !== true) {
  // define all the functions
  // ...
  utilitiesHasRun = true;
}

Are there any other options?

like image 707
Shawn Avatar asked Jun 14 '12 14:06

Shawn


People also ask

How do you write a conditional in JavaScript?

In JavaScript we have the following conditional statements: Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.

How do you define a function in JavaScript?

A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (). Function names can contain letters, digits, underscores, and dollar signs (same rules as variables). The parentheses may include parameter names separated by commas: (parameter1, parameter2, ...)

Can you call a function before it has been defined JavaScript?

Is hoisting actually "calling the function before it is defined"? Raising the scope of a declared name so that it will be defined for other code under that scope -- that's roughly what JavaScript does to allow you to call the function before it's declared, and that's hoisting.

How many conditional statements are used in JavaScript?

In JavaScript we have three conditional statements: if statement - use this statement if you want to execute a set of code when a condition is true. if...else statement - use this statement if you want to select one of two sets of lines to execute.


1 Answers

You can make them function expressions instead of function declarations:

if (false)​ {
    var foo = function () {
        alert("FOO");
    };
}
foo(); //TypeError: undefined is not a function

Note that in the above code, foo is still accessible even though the condition evaluated to false. This is because function-scoped declarations (both function and var) are hoisted to the top of the scope in which they are declared. However, assignments happen at the point in the code where they appear.

What the above code is effectively doing is this:

var foo; //No assignment, foo is undefined
if (false)​ {
    foo = function () {
        alert("FOO");
    };
}
foo(); //TypeError: undefined is not a function

Warning - Named function expressions are still hoisted in Internet Explorer 8 and below (this is a bug in IE). Obviously this is only a potential problem if you need to support old browsers.

like image 142
James Allardice Avatar answered Oct 19 '22 23:10

James Allardice