Every time a modal is opened I load via ajax some html + js, like this:
var loadModalJs = function() {
...
}
$(function() { loadModalJs(); });
It works with var
, but if I replace var
with let
, when the modal is closed and then opened again I get Identifier 'loadTabJs' has already been declared
I tried something like this, without success:
if(typeof loadModalJs === 'undefined') {
let loadModalJs = function() {
...
};
}
loadModalJs(); // ReferenceError: loadModalJs is not defined
I don't like using var because my IDE complains about "'var' used instead of 'let' or 'const'"... But none of them seems to work... How could I do?
Use the typeof operator to check if a function is defined, e.g. typeof myFunction === 'function' . The typeof operator returns a string that indicates the type of a value. If the function is not defined, the typeof operator returns "undefined" and doesn't throw an error.
Summary. In JavaScript, a variable can be either defined or not defined, as well as initialized or uninitialized. typeof myVar === 'undefined' evaluates to true if myVar is not defined, but also defined and uninitialized. That's a quick way to determine if a variable is defined.
let can be updated but not re-declared. This is because both instances are treated as different variables since they have different scopes. This fact makes let a better choice than var . When using let , you don't have to bother if you have used a name for a variable before as a variable exists only within its scope.
let and const are block-scope variable declarations that can replace 'var' declarations in many cases.
Whenever you create a variable and do not assign any value to it, by default it is always undefined. It becomes defined only when you assign some value to it. There are numerous ways to check if a variable is not defined. We are going to use one of the easiest solutions which involve the usage of the typeof and ternary (?) operators.
Finally, the function is not defined error can also be caused by calling the function before the script is loaded to the browser. Suppose you have a JavaScript file separated from your HTML file as follows: Then you load the script into your HTML file, but you call the fnAction function before you load the script as follows:
Please keep in mind that functions defined through function expressions must be defined before the call. Function expressions are functions that you defined through a variable keyword as follows: From the example code above, the variable fnAction will be hoisted, but the function declaration is not, so it will be undefined as shown below:
Function expressions are functions that you defined through a variable keyword as follows: From the example code above, the variable fnAction will be hoisted, but the function declaration is not, so it will be undefined as shown below: That’s why it’s always better to define the function before calling it.
The error you placed into the comment is correct: at no point do you define the variable in scope -- you define it within the the if
clause, and it is not available outside of that clause, your reference to it in the if
statement is to an undefined global variable.
Also, if your variable is only ever going to be undefined
or a function
, then no need to test its type, as undefined
evaluates to false.
let loadModalJs;
// ...your other code here...
// This bit within the function/scope run multiple times:
if (!loadModalJs) {
loadModalJs = function() {
...
};
}
if (loadModalJs) {
loadModalJs();
}
Your second snippet could work if you use var
instead of let
.
As if
creates its own block {}
:
let
is limited to the immediate block {}
.var
is limited to the immediate function block {}
(or global if there are no functions).E.g.:
if (true) {
var x = "It'll work outside the if.";
}
console.log(x);
As an additional tip, you might be careful with the way you 'create' functions in javascript:
let myFunction = function() { }
, are under top-to-bottom flow of control (i.e. first define, then use).function myFunction() { }
, are hoisted (i.e. declare and use where you want, in the same scope).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With