JavaScript Should you ever use local inner functions?

Which of these is normally seen as better when writing JavaScript. Foo, bar, and baz are specific to this function so won't be used anywhere else in other functions.

function() {
    foo();
    bar();
    baz();
}

function foo() {
    //lines of code
}

function bar() {
    //lines of code
}

function baz() {
    //lines of code
}

Or

function() {
    function foo() {
        //lines of code
    }
    foo();
    function bar() {
        //lines of code
    }
    bar();
    function baz() {
        //lines of code
    }
    baz();
}

The advantages of the first is it is more efficient since if you are calling the function more that once the other functions only need to be created once. It is also easier to read as the main outer function is now only 3 lines long and not much longer.

The only advantage I can think for the second is it keeps these functions private to the outer function and not accessible to the rest of the file which is good since these functions will not be used anywhere else. Obviously normally making things public when they shouldn't need to be is bad but is that less important in JavaScript situations like this.

Is it possible you would choose differently depending on how often the function was called or anything else?

Answer:1

my question depends on the following scenario: I have an JS script, which adds a span element into a div. As the div has a text-align:middle;, the already existing spans are "jumping" to the left or ...

my question depends on the following scenario: I have an JS script, which adds a span element into a div. As the div has a text-align:middle;, the already existing spans are "jumping" to the left or ...

I have a simple table in which there are 2 columns: a) Name b) Country. Currently it works fine and displays name of the user and country. Now I want to add a condition so that if the country is ...

I have a simple table in which there are 2 columns: a) Name b) Country. Currently it works fine and displays name of the user and country. Now I want to add a condition so that if the country is ...

  1. data manipulation angularjs

I have the following set up: And I am trying to do the following: case 'UPDATE_PASSWORD': return { ...state, //preserve current state, apply changes to it below password: action.password,...

I have the following set up: And I am trying to do the following: case 'UPDATE_PASSWORD': return { ...state, //preserve current state, apply changes to it below password: action.password,...

Is there a better way to run parallel queries with the RethinkDB Node driver than opening multiple connections per request? Or is this actually a good way of going about what I need? I'd prefer to ...

Is there a better way to run parallel queries with the RethinkDB Node driver than opening multiple connections per request? Or is this actually a good way of going about what I need? I'd prefer to ...

  1. rethinkdb nodejs
  2. rethinkdb nodejs example
  3. rethinkdb nodejs npm
  4. rethinkdb nodejs driver
  5. rethinkdb node js tutorial
  6. rethinkdb node.js socket.io
  7. rethinkdb node js module
  8. rethinkdb node js setup
  9. rethinkdb node js orm
  10. rethinkdb node js express
  11. rethinkdb example node js chat
  12. node.js rethinkdb example
  13. changefeeds rethinkdb nodejs
  14. nodejs rethinkdb socket.io
  15. nodejs rethinkdb tutorial
  16. rethinkdb connection node js
  17. nodejs rethinkdb cursor
  18. nodejs rethinkdb chat
  19. nodejs use rethinkdb
  20. nodejs rethinkdb express