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?
let loadModalJs
, hence the error. You probably want to only declare it once or at the very least just re-assign it. It's hard to see where the re-declaration is happening because it's not included in this code.loadModalJs
need to be declared again? Maybe it's as simple as making a single function that takes a parameter and calling it with a given value, instead of making a new function every time.window.function
if it realy need to be a global function (bad idea, keep the global scope as clean as possible), 2)let
is block-scope spectifi. if you declare a function/variable inside a block ({...}
) its only available in this specific block/scope.