Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So you have basically three options: </p> <ul> <li>You want to expose settings as a global variable, and have it accessed by your functions,</li> <li>You want to hide settings as an implementation detail and have it accessed by your functions,</li> <li>You want to give a possibility to supply different settings objects to different functions.</li> </ul> <p><strong>Global variable approach</strong></p> <p>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.</p> <p>That said, because of all the naming conflicts, in Javascript it's good to "namespace" all the global variables. So instead of global variables <code>foo</code> and <code>bar</code> you should rather have one global variable <code>MyGlobalNamespace</code> which is an objects having attributes: <code>MyGlobalNamespace.foo</code> and <code>MyGlobalNamespace.bar</code>. </p> <p><strong>Private variable approach</strong></p> <p>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.</p> <p><strong>Additional function parameter approach</strong></p> <p>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.</p> <p><strong>EDIT</strong></p> <p>As to the question from the comments:</p> <p>Example 1)</p> <pre><code>var blah = 123; function fizbuzz() { console.log(blah); // &lt;-- This is an example of a closure accessing // a variable console.log(this.blah); // &lt;-- 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. } </code></pre> <p>Example 2)</p> <pre><code>var obj = { blah: 123, fizbuzz: function() { console.log(this.blah); // &lt;-- 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); // &lt;-- This MAKES NO SENSE, there is no variable blah // accessible from here. } }; </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload