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 4608868
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:54:21+00:00 2026-05-22T00:54:21+00:00

Crockford had this example to keep myArray from being in the global scope: var

  • 0

Crockford had this example to keep myArray from being in the global scope:

var myName = (function() {
    var myArray = ['zero','one','two','three','four'];
    return function(X) {
        return myArray[X];
    }
}()); // This function is invoked immediately

result = myName(3); // Now invoke it "for real"

Q: I don’t get why it isn’t

var myName = (function(X) {

Q: When I call myName(3), isn’t “var myArray=” executed a 2nd time?
Suppose it’s not executed a 2nd time because JavaScript knows that it’s already been defined… What about a loop or some other logic between the var stmt and the return function stmt? Wouldn’t it be executed every time?

Q: Can you name the subfunction and call it instead of calling myName?

  • 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-05-22T00:54:21+00:00Added an answer on May 22, 2026 at 12:54 am

    okay, let’s break this down…

    var myName = (function(){
        ...
    }());
    

    that piece sets myName to whatever that anonymous function returns, so if it were:

    var myName = (function(){ return 42; }());
    

    myName would equal 42. If that doesn’t make sense, this is the same thing:

    function someFunction(){ return 42; }
    var myName = someFunction();
    

    So in your example, myName is set to function(X){ return myArray[X] }. So myName is a function. When you call it, the only code that is run is return myArray[x]. myArray is kept in what is called a closure, it is only exposed to the myName function and the anonymous one surrounding it.

    I wrote an article on closures years back that may help you: http://www.htmlgoodies.com/primers/jsp/article.php/3606701/Javascript-Basics-Part-9.htm (scroll down to the “Closures” header).

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

Sidebar

Related Questions

Douglas Crockford seems to like the following inheritance approach: if (typeof Object.create !== 'function')
I've adapted the Crockford object() function so that I can pass in some parameters
At the moment I'm reading Douglas Crockford's book, and the towers of hanoi function
Beginner in JS :) needs an explanation of code piece from Crockford's book ,
I heard (from Crockford) what type attributes on LINK and SCRIPT elements are superfluous
I've been watching Douglas Crockford's talks at YUI Theater, and I have a question
On Stackers' recommendation, I have been reading Crockford's excellent Javascript: The Good Parts .
I'm looking for a good example of using Regular Expressions in PHP to reverse
This is going to be a little long and rambly, but I want to
I'm looking into development standards fro JavaScript. I had previously used Doulgas Crockfords Javascript

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.