0

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?

I Hate Lazy
  • 47,415
  • 13
  • 86
  • 77
Martina
  • 1,852
  • 8
  • 41
  • 78

4 Answers4

3

You haven't closed your if statement on the second line.

dsgriffin
  • 66,495
  • 17
  • 137
  • 137
  • yes, i've putted the closing brachets in another part of the code. But now the question is: why with chrome and ie I did not have problema? – Martina Dec 07 '12 at 14:35
  • The `if` statement is closed. Clearly OP wants it to be conditional. – I Hate Lazy Dec 07 '12 at 14:36
  • @user1856906 Read dystroy's point about browser interpretation. – dsgriffin Dec 07 '12 at 14:39
  • Sure it's closed. Indentation doesn't define how brackets are paired. OP just excluded the closing `}` that belongs to the outer function. – I Hate Lazy Dec 07 '12 at 14:40
  • 1
    the function was closed after the definition of the function disegnaIconaIngredienti() .with chrome and ie worked but with firefox no. Closing the if before the definition of the function disegnaIconaIngredienti() now works also on firefox. – Martina Dec 07 '12 at 14:43
  • @user1856906: Right, changing the logic if your code makes it work. If you didn't want it to be conditional, then why did you have it in the `if`? If you *do* want it to be conditional, then keep the `if` closed where it is, and use different syntax for creating the function. – I Hate Lazy Dec 07 '12 at 14:48
2

Your code is equivalent in Firefox to

var disegnaIconaIngredienti;
if (prodotto.approvatoIngredienti==true){
   disegnaIconaIngredienti();
   disegnaIconaIngredienti = function(){
      //
   }
}

So the variable doesn't have a value when you call it.

Chrome and Internet Explorer hoist the whole function declaration and not only the variable declaration.

ECMAScript doesn't allow function definition in non function blocks (like your if). Browsers allow it but in different ways.

This related question goes deeper in the topic.

Community
  • 1
  • 1
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
1

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 if statement, 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.

function a(){
    if(prodotto.approvatoIngredienti == true) {

                   // legal function in a block
         var disegnaIconaIngredienti = function() {
            //
         };
         disegnaIconaIngredienti();
     }
I Hate Lazy
  • 47,415
  • 13
  • 86
  • 77
0

You forgot to close the if brace

function a(){
if(prodotto.approvatoIngredienti==true){
    disegnaIconaIngredienti();
}

function disegnaIconaIngredienti() {
     //
     }
 }

After further consideration on your closing if and @dystroy's post I tend to agree with him and say that Firefox doesn't permit using

part of his post to answer :

ECMAScript doesn't allow function definition in non function blocks (like your if). Browsers allow it but in different ways.

Like in your case where Firefox doesn't allow it inside conditional blocks.

Lucian Enache
  • 2,510
  • 5
  • 34
  • 59