up vote 2 down vote favorite

This work:

var stepFunc = 
[
    //Step 0 (it doe not exist)
    function(){console.log("Step 0");},
    //Step 1
    (function(){
        var fFirstTime = true;
        return function(){
            console.log("Step 1");
            if (fFirstTime){
                //Do Something
            }
        }   
    })(),
    // ... Other steps
];

This does NOT work :

var stepFunc =
[ // Step 0 
    [
        function(){console.log("Step 0");},
        function(){console.log("Check Step 0");} 
    ], //Step 1         
    (function(){
        var fFirstTime = true;          
        return
        [
            function() { //Initialization function
                console.log("Step 1");
                if (fFirstTime){
                    //Do somrthing
                }
            }, function() { 
                return true;
            }
        ];      
    })(), 
    // ...
];

I would like that stepFunc would be an array of arrays of functions. On the first level I want to create a closure that has its own data. Why is stepFunc[1] "undefined" ?

link|flag
1  
Did my best to clean this up, fix the indenting, and line up the brackets; you should try to balance your brackets even in sample code snippets, otherwise people will assume that is what is causing whatever your problem is. – meagar Nov 17 at 2:21
1  
@Šime Vidas - I think you inadvertently removed the cause of the issue in your edit. Be sure to maintain the integrity (or lack thereof) of the code in the question. :o) Rolling back to @meagar's edit. – patrick dw Nov 17 at 2:31
@patrick Haha, I removed the issue by just trying to format the code better in order to be able to read the code so that I can try to solve the issue. Ah, JavaScript. :) – Šime Vidas Nov 17 at 2:45
I tested it. I copied and pasted in jconsole.com and use the developer tool. stepFunc[1] IS "undefined". What is the problem? – Chris Cinelli Nov 17 at 2:47
2  
@Chris Check out jslint.com Put your code trough it and it will report errors that you probably want to fix in order to make your code more robust. – Šime Vidas Nov 17 at 2:53
show 4 more comments

1 Answer

up vote 5 down vote accepted

You're probably stumbling over implicit statement termination, a.k.a. implicit semicolon insertion. The line after return is ignored completely and nothing is returned. This works:

var stepFunc = [
            // Step 0 
            [
                function(){console.log("Step 0");},
                function(){console.log("Check Step 0");} 
            ], //Step 1      
           (function(){
               var fFirstTime = true;          
               return [  // <-- important to begin the return value here
                  function() {
                    console.log("Step 1");
                    if (fFirstTime){
                        //Do somrthing
                    }
                  }, function()   { 
                    return true;
                  }
               ];         
           })()
];
link|flag
2  
One of the most evil parts of javascript according to Mr. Douglas Crockford... – CrazyJugglerDrummer Nov 17 at 2:18
@deceze I was about time that I see this happen to someone :p Not just in theory. – Šime Vidas Nov 17 at 2:31
I do want that nothing after return is executed. It is actually the only the last statement – Chris Cinelli Nov 17 at 2:39
@Chris - But you do want to return the Array holding the functions, right? Otherwise, what's the purpose of the self-invoking function? – patrick dw Nov 17 at 2:44
I want to be able to call later in the code stepFunc[1][0](); – Chris Cinelli Nov 17 at 2:49
show 4 more comments

Your Answer

 
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.