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

  • Home
  • SEARCH
  • 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 6643059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:00:16+00:00 2026-05-26T00:00:16+00:00

another question concerning javascricpt closures. I have a global settings object. Is it better

  • 0

another question concerning javascricpt closures. I have a global “settings object”. Is it better to use it from within functions in global scope or pass the object every time a function needs to access the object?

For a better understanding a little mockup of the situation here

Please ignore that “baz()” also gets the passed object within “foobar()” from the global scope within the closure. You see that both versions work fine.

The thing is that I am passing whatever object the function needs to work to every function and EVERY time (unneccesary overhead?), which might be nice and easy to read/understand but I am seriously thinking about changing this. Disadvantage would be that I have to keep the “this” scope wherever it gets deeper right?

Thanks for your advice!

  • 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-26T00:00:17+00:00Added an answer on May 26, 2026 at 12:00 am

    So you have basically three options:

    • You want to expose settings as a global variable, and have it accessed by your functions,
    • You want to hide settings as an implementation detail and have it accessed by your functions,
    • You want to give a possibility to supply different settings objects to different functions.

    Global variable approach

    Well, I guess it’s a little bit like with any global variable. If settings is a singleton (for example it describes your application) and you can’t see any benefit in having a possibility to call the same function with different settings objects, then I don’t see why it couldn’t be a global variable.

    That said, because of all the naming conflicts, in Javascript it’s good to “namespace” all the global variables. So instead of global variables foo and bar you should rather have one global variable MyGlobalNamespace which is an objects having attributes: MyGlobalNamespace.foo and MyGlobalNamespace.bar.

    Private variable approach

    Having a private variable accessed by a closure is a good pattern for hiding implementation details. If the settings object is something you don’t want to expose as an API, this is probably the right choice.

    Additional function parameter approach

    Basically if you see a gain in having a possibility of supplying different settings to different function calls. Or perhaps if you can picture such gain in the future. Obvious choice if you have many instances of settings in your application.

    EDIT

    As to the question from the comments:

    Example 1)

    var blah = 123;
    
    function fizbuzz() {
        console.log(blah); // <-- This is an example of a closure accessing 
                           // a variable
        console.log(this.blah); // <-- Most likely makes no sense. It might work, 
                                // because by default this will be set to a global 
                                // object named window, but this is probably not 
                                // what you want. In other situations this might 
                                // point to another object.
    }
    

    Example 2)

    var obj = {
    
        blah: 123,
    
        fizbuzz: function() {
            console.log(this.blah); // <-- This is *NOT* an example of a closure
                                    // accessing a private variable. It's rather the
                                    // closest Javascript can get to accessing an
                                    // instance variable by a method, though this 
                                    // terminology shouldn't be used.
            console.log(blah); // <-- This MAKES NO SENSE, there is no variable blah 
                               // accessible from here.
        }
    };
    

    In a nutshell, I’d encourage you to read some good book on the fundamental concepts of Javascript. It has its paculiarities and it’s good to know them.

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

Sidebar

Related Questions

This question is similar to another one, concerning pyCURL I have the following error
Another question related to this one . I have a List<SortableObjects> that is the
I have a feeling someone is going to point me to another question that
Here is a link to another question I asked concerning the same project I
I'm back with another question concerning threads and synchronization. Imagine a server application that
I have a question concerning user defined types in DB2(v. 9.7.0.441). I want to
I have a question concerning a generic type parameter. Let's say I have the
Another question about my implementation. I am fairly new to java/android coming from a
I have a question concerning the structure of an MVC application. Suppose we have
I have a question concerning cookie storage design. I am developing a web application

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.