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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:37:45+00:00 2026-06-15T18:37:45+00:00

So with the following code, I proved to myself that nested functions do indeed

  • 0

So with the following code, I proved to myself that nested functions do indeed gain a copy of the parameters of the outer function:

    var o = {};
    (function(a,b,c,x){
        x.f = function(){
            return a.toString()+b.toString()+c.toString();
        }
    })(7,4,2,o);
    o.f();

The code yields

    742

Which means that the o.f function gains a copy of a,b, and c from the anonymous function. Otherwise, I would just get undefinedundefinedundefined

My questions are,

  • First, is this always true? or are there strict circumstances? (Like, must it be an object? etc.)
  • Also, what other kinds of obscure scopes exist like this in javascript? I’d love to know (i.e. what about third iterations?)
  • Lastly, I’d be perfectly fine with reading a document that explicates advanced concepts on javascript scopes. Does anyone know of any good ones?
  • 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-15T18:37:46+00:00Added an answer on June 15, 2026 at 6:37 pm

    What you observe is called lexical scope. It means that the bindings of the variables in a certain scope in JavaScript are determined by where the variables appear in the code. It is always true, and it is true up to any level. The only main exception is the this value, which is dynamically scoped rather than lexically scoped. Dynamic scope means variables in the function depend on how and when the function is called. (See Lexical Scoping and Dynamic Scoping.)

    Example:

    var o = { 
        a: function() {
            var x = 5;
            console.log(this, x);
            function b() {
                console.log(this, x);
            }
            b();
        }
    };
    
    o.a();
    

    The result of this example will be:

    {Object o} 5
    {window} 5
    

    In other words, the first console.log will log this as a reference to the o object, while the second console.log will log this as a reference to the window object. However, both will log x as being equal to 5. The value of this is window when it is called in non-strict mode without a context. So this is not scoped lexically, but other variables, like x are. To read more about the behavior of this see A Short Overview of this.


    To answer your questions directly:

    1. First, is this always true? or are there strict circumstances? (Like, must it be an object? etc.)

    Yes, it’s true with the exception of this and arguments which change based on how the function is called. It doesn’t have to be an object, all variables are lexically scoped.


    2. Also, what other kinds of obscure scopes exist like this in javascript? I’d love to know (i.e. what about third iterations?)

    You can go as deep as you want — inner functions can always access the variables of their outer functions.

    function a() {
        var x = 1;
        console.log('a:', x);
        return function b() {
            var y = 2;
            console.log('b:', x, y);
            return function c() {
                console.log('c:', x, y);
            };
        };
    }
    
    var foo = a();   // => logs 'a: 1'
    var bar = foo(); // => logs 'b: 1 2'
    bar();           // => logs 'c: 1 2'
    

    This is actually part of another topic referred to as closures, which occur when you return a function from within another function.


    3. Lastly, I’d be perfectly fine with reading a document that explicates advanced concepts on javascript scopes. Does anyone know of any good ones?

    I’ve linked to a couple resources already. Another good one:

    MDN: Functions and function scope (specifically the section on Nested Functions and Closures).

    Also, you would be benefited from reading anything on closures, and you may also want to look up lexical scope.

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

Sidebar

Related Questions

I've got code that uses the function 'time' and other functions from 'time.h', and
Following code produces a nested array as a result for keys containing three items:
Let's say I have the following code: typedef std::function<void ()> func_type; void some_func() {
I have the following code: CREATE OR REPLACE FUNCTION repeatable_rand_text(ftype IN VARCHAR2 , in_val
Does the following code render the using(...) function/purpose irrelevant? Would it cause a deficiency
Take as an example the following C# function: static void Main(string[] args) { var
I've the following code that uses jQueryMobile Sliders: <!DOCTYPE html> <html xmlns=http://www.w3.org/1999/xhtml> <head> <meta
I have the following code that evaluates to true in the emulator (OS 2.3.3)
I have encountered the following problem which proved to me that I know far
I've been following a spine in javascript sample code that that I've found. It

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.