1

Under what circumstances would I choose to do this:

function foo(){
    
    function setup(){
        // do some stuff   
    }
    
    function moreSetup(){
        // and more and more
    }
    
    function init(){
        // init stuff
    }
    
    init();
}

And under what circumstances would I prefer this?

function foo(){
    
    function setup(){
        // do some stuff   
    }
    
    function moreSetup(){
        // and more and more
    }
    
    function init(){
        // init stuff
    }
    
    return{       
        init();
    }
}
1
  • No worries. :) If you didn't ask, you'd still be confused. Commented Oct 31, 2010 at 13:25

4 Answers 4

1

Your second example is invalid:

return{       
        init();
    }

The { ... } defines an object literal which must contain key-value pairs, like { foo: 'bar' }.

Sign up to request clarification or add additional context in comments.

Comments

1

Your first example will do the stuff within init and return undefined.

Your second example is invalid. I think you wanted:

return init()

without curly braces. This will return the result of init (which you haven't specified). In both cases, setup and moreSetup never get called, so I'm not sure why they're there.

You've left out a lot of details, but my answer right now is: if the caller needs to do something with the result of init you'll need to go with option #2.

Comments

0

No offense meant, but this seems like a somewhat confused question. As others have noted, you shouldn't have the brackets around init() in the second example. And when you say function setup() { ... } you're just defining the function, not calling it -- meaning in this example the function never gets called.

But the main thing I'm posting this answer to try to address is this: "Call init function vs. returning that function call". It is incorrect to think that you are "returning the function call" in the second example. When you say return init(), first init() is called and returns some value, and then your foo function returns the value returned by init().

So that's the question you should be asking. Do I just want foo to call init, or do I want foo to call init and return the value that init returns? That's the difference between the two (once the other errors are removed).

Comments

0

If you are still thrilled by the idea of calling your function with init, like I do :), then you could design your function as follows:

var mainFunction = function() {
  var init = () => {
    functionOne();
    functionTwo();
  }

  function functionOne(){
    console.log('I am the first one');
  }

  function functionTwo(){
    console.log('I came second?');
  }

  return {
   init: init
  };
}(); // please note the () - call the function here or use
// mainFunction().init()

mainFunction.init();

Comments

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.