Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8389101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:37:57+00:00 2026-06-09T18:37:57+00:00

I decided to make one step forward on trying to understand Javascript and read

  • 0

I decided to make one step forward on trying to understand Javascript and read again Javascript: The Good Parts. And here comes the first doubt:

Let’s say I want to avoid using the global variables because they are evil, and so I have the following:

var digit_name = function(n) {
 var names = ['zero','one','two','three'];
 return names[n];
}

D.Crockford claims that this is slow because everytime the function gets called, a new instantiation of names is done. So, then he moves to the closure solution by doing this:

var digit_name = function () {
  var names = ['zero', 'one', 'two', 'three'];
  return function (n) {
    return names[n];
  }
}();

This makes the names variable stored in memory and therefore it doesn’t get instantiated every time we call digit_name.

I want to know why? When we call digit_name, why is the first line being “ignored”? What am I missing? What is really happening here?

I have based this example not just in the book, but on this video (minute 26)

(if someone thinks of a better title, please suggest as appropriate…)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:37:59+00:00Added an answer on June 9, 2026 at 6:37 pm

    I’m sure you meant to make your second example function an immediate executing (i.e., self-invoking) function, like this:

    var digit_name = (function () {
      var names = ['zero', 'one', 'two', 'three'];
      return function (n) {
        return names[n];
      }
    })();
    

    The distinction involves scope chain with closures. Functions in JavaScript have scope in that they will look up into parent functions for variables that are not declared within the function itself.

    When you declare a function inside of a function in JavaScript, that creates a closure. A closure delineates a level of scope.

    In the second example, digit_name is set equal to a self-invoked function. That self-invoked function declares the names array and returns an anonymous function.

    digit_name thus becomes:

    function (n) {
      //'names' is available inside this function because 'names' is 
      //declared outside of this function, one level up the scope chain
      return names[n];
    }
    

    From your original example, you can see that names is declared one up level up the scope chain from the returned anonymous function (which is now digit_name). When that anonymous function needs names, it travels up the scope chain until it finds the variable declared–in this case, names is found one level up the scope chain.

    Regarding efficiency:

    The second example is more efficient because names is only declared once–when the self-invoking function fires (i.e., var digit_name = (function() { … })(); ). When digit_names is called, it will look up the scope chain until it finds names.

    In your first example, names gets declared every time digit_names is called, so it is less efficient.

    Graphical example:

    The example you’ve provided from Douglas Crockford is a pretty tough example to start out with when learning how closures and scope chains work–a lot of stuff packed into a tiny amount of code. I’d recommend taking a look at a visual explanation of closures, like this one: http://www.bennadel.com/blog/1482-A-Graphical-Explanation-Of-Javascript-Closures-In-A-jQuery-Context.htm

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I normally create apps for the iPad, but I decided to make one for
So I have a huge program and decided I should make one of the
One of my non-programmer friends recently decided to make a C++ program to solve
I've finally decided to make some tests for my apps but I'm stuck on
Ok this time I decided to make a list using the STL. I need
This question is about rewriting git history. Six months ago I decided to make
I am learning mysql join queries. To pratice, I decided to make tables for
Having searched a whole lot of similair posts, workarounds, I decided to make my
I installed the latest GNU make to my windows machine. The installer decided to
I needed to make a window smaller than the OS minimum, I decided to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.