2

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" ?

8
  • 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. Commented Nov 17, 2010 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. Commented Nov 17, 2010 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. :) Commented Nov 17, 2010 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? Commented Nov 17, 2010 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. Commented Nov 17, 2010 at 2:53

1 Answer 1

6

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;
                  }
               ];         
           })()
];
Sign up to request clarification or add additional context in comments.

7 Comments

@deceze I was about time that I see this happen to someone :p Not just in theory.
I do want that nothing after return is executed. It is actually the only the last statement
@Chris - But you do want to return the Array holding the functions, right? Otherwise, what's the purpose of the self-invoking function?
I want to be able to call later in the code stepFunc[1][0]();
@Chris, The line break after return caused the error. When you have a line-break after a return statement, javascript interpreters think it is the end of the statement, and insert a ;. Be aware that @deceze removed part of the outer Array, so you'll need to add that back in.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.