Take the 2-minute tour ×

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!

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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.


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.

share|improve this answer
Talking about namespacing js vars: I did indeed namespace the closure itself. You wrote namespace "foo" and "bar". I think what you mean is just "Foo", right? Because "bar", "baz" and "foobar" are just "private" functions and whatever is within the closure does not need to be namespaced? Plus, I could make sure to always call variables inside the closure by using "this" right? That's another thing, I can use either "this.settings" or "setting", works both but the first one makes sure that I access the variable inside the closure (might be another global "setting" variable lurking somewhere)? –  junior Sep 27 '11 at 10:11
It was meant as an example, my foo and bar were not supposed to have anything to do with your foo and bar. –  julkiewicz Sep 27 '11 at 10:14
Okay then I got your point, thanks. –  junior Sep 27 '11 at 10:21
Any ideas on good javascript books? –  junior Sep 27 '11 at 12:24
@junior I'd definitely agree with this answer: stackoverflow.com/questions/74884/good-javascript-books/… . The book is great, there are also lectures by him on the web. –  julkiewicz Sep 27 '11 at 14:18
add comment

Your settings object isn't global, it's defined within a closure.

As the intent is that it be accessed by other function within the closure, I'd say you were 100% fine to just access that object directly and not pass it as a parameter.

FWIW, even if you did pass it as a parameter the overhead is negligible because only a reference to the object is passed, not a copy of the whole object.

share|improve this answer
add comment

Your Answer


