74

(Maybe) I just solved a my problem (How to update front-end content after that a form is successfully submitted from a dialog window?) by "storing" / "saving" a variable in the JavaScript window object. However, since I am newbie in JavaScript matters, I have some doubts if storing / saving a variable in the JavaScript window object is a "common" / "proper" way to use that object. Is it?

For example, using the following code

$('.trigger').click(function() {
  window.trigger_link = this;
});

is advisable?

3 Answers 3

125

In JavaScript, any global variable is actually a property of the window object. Using one is equivalent to (and interchangeable with) using the other.

Using global variables is certainly "common," so the question is whether or not it's "proper." Generally, global variables are discouraged, because they can be accessed from ANY function and you risk having multiple functions trying to read from and write to the same variables. (This is true with any programming language in any environment, not just JavaScript.)


Solve this problem by creating a namespace unique to your application. The easiest approach is to create a global object with a unique name, with your variables as properties of that object:

window.MyLib = {}; // global Object container; don't use var
MyLib.value = 1;
MyLib.increment = function() { MyLib.value++; }
MyLib.show = function() { alert(MyLib.value); }

MyLib.value=6;
MyLib.increment();
MyLib.show(); // alerts 7

Another approach is to use .data() to attach variables to a relevant DOM element. This is not practical in all cases, but it's a good way to get variables that can be accessed globally without leaving them in the global namespace.

2
  • Any idea why window variables are not accessible in firefox extension option page? Commented Apr 13, 2022 at 14:32
  • Good solution. But as the object is accessible from everywhere it can be changed everywhere. I have a use-case that calls for global constant settings. One way to achieve this is to freeze() the object if it should no longer be modified.
    – theking2
    Commented Jul 1 at 18:54
10

What is actually not mentioned here, and is probably the biggest deal breaker on why not to use window as global scope carrier is that it can be frozen (not writable) in some cases (and I'm talking from production based experience).

A good pattern is actually just create a global variable that will be used for all the common stuff in your application

var Application = {};
Application.state = { name: 'Application' }
.
.

What I found as the best pattern for me in javascript is to have a global state using Redux.

3
  • 22
    Can you describe when exactly could the window be frozen?
    – dagoltz
    Commented Jun 24, 2019 at 7:33
  • I like to use a name that is as safely unique as possible without being too cryptic. Application is a fairly generic name which could be used by some other script and assigning it to an empty object would override what was already there. I am coming to this post because of Adobe Analytics which uses a window.s object which I believe is a horribly generic global identifier. It has likely remained so because of legacy purposes. There is also the myriad of cases where code conflicts with $ which jQuery uses.
    – King Holly
    Commented Nov 3, 2020 at 1:06
  • I haven't seen the comments with the questions until now, so I don't remember what was the cause, I know I had issues with Safari in private mode. King Holly I agree, in retrospective Application is too generic term and it would be far better to use your product/application name.
    – D.J.
    Commented Nov 3, 2020 at 9:08
3

Another pattern to make a unique namespace in your application.

(function myApp()
{
    var value = 5;
    function addToValue(x)
    {
         value += x;
    }
})()

If you want functions/values to be accessible afterwards you can store them in an object like this:

function myApp()
{
    this.value = 5;
    this.addToValue = function(x)
    {
         this.value += x;
    }
}
var myInstance = new myApp();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.