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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:30:28+00:00 2026-06-17T09:30:28+00:00

[JavaScript functions] internally store any variables they may refer to that are defined in

  • 0

“[JavaScript functions] internally store any variables they may refer to that are defined in their enclosing scopes.”

How can I determine what that set of variables is?

For example, David Herman in Effective JavaScript gives this function (and closure):

function sandwichMaker() {

    var magicIngredient = "peanut butter";

    function make(filling) {
        return magicIngredient + " and " + filling;
    }

    return make;
}

var f = sandwichMaker();

document.writeln("<p>" + f("jelly") + "</p>");
document.writeln("<p>" + f("bananas") + "</p>");
document.writeln("<p>" + f("marshmallows") + "</p>");

Sure, magicIngredient is a variable accessible to make(), but what else is? What if sandwichMaker were itself within a function? And then there are the globals. What is the function looking at when it looks for relevant values within the current scope?

  • 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-17T09:30:29+00:00Added an answer on June 17, 2026 at 9:30 am

    What if sandwichMaker were itself within a function? And then there are the globals.

    Yes, all the variables from parent functions are accessible (if they are not shadowed). And the highest-scoped function inherits from the global scope then.

    What is the function looking at when it looks for relevant values within the current scope?

    You can inspect that with a debugger. Insert a debugger; statement in make, and then execute it and have a look into your devtools. You will see something like this:

    Scope Chain
        0. make (currently executed):
            this: (+)Window
            arguments: (+)Arguments
            filling "jelly"
        1. sandwichMaker:
            arguments: (+)Arguments
            magicIngredient: "peanut butter"
            make: (+)Function
        Global
            AnonXMLHttpRequest: …
            ApplicationCache: …
            Array: …
            …
    

    Also have a look at this great article: http://dmitrysoshnikov.com/ecmascript/es5-chapter-3-2-lexical-environments-ecmascript-implementation/

    Example view at Chrome Devtools:

    (from http://www.briangrinstead.com):

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

Sidebar

Related Questions

I need 2 simple JavaScript functions that read and change the color of a
I had a problem with some JavaScript functions that had me scratching my head
I have several onmouseover events (combination of javascript functions and jquery accordions) but they
How can I check whether all JavaScript functions have loaded properly on a page?
I have Javascript that people are including in their page. In my Javascript I
We have a javascript function we use to track page stats internally. However, the
How do I compare 2 functions in javascript? I am not talking about internal
I have multiple javascript functions in my html page. One of them, by far
What is the difference between the JavaScript functions decodeURIComponent and decodeURI ?
I want to use PHP to replace javascript functions in HTML documents. For example:

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.