I have this code:
function a() {
if(prodotto.approvatoIngredienti==true) {
disegnaIconaIngredienti();
function disegnaIconaIngredienti() {
//
}
}
I defined a function inside another function. With chrome and ie I don’t have problem, but firefox gives me this error:
--
[15:26:41.279] disegnaIconaIngredienti is not defined @ http://127.0.0.1:8080/Tesi/javascript/InserimentoProdotti.js:1718
Someone can explain me why?
It’s because firefox has something called function statements. They’re different from typical declarations, and can legally happen in a block.
There’s no hoisting of the function itself as you’d find with a declaration, so it needs to be defined before it’s used.
Note that in typical ECMAScript, it’s invalid to have that style of function inside an
ifstatement, though some browsers allow it. Strict mode absolutely prohibits it.To have a fully valid function created inside an
if, it must be a function that is part of an expression, like an assignment.