Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing a variable in the JavaScript 'window' object is a proper way to use that object?

(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?

like image 431
Backo Avatar asked Sep 12 '12 17:09

Backo


People also ask

Are variables stored in the window object?

In JavaScript, any global variable is actually a property of the window object.

Which of this is a correct way to define a variable as global using window object?

Correct way to declare global variable in JavaScript The proper way is to use window object. And use the syntax like this: var window. iAmGlobal = "some val" ; //Global variable declaration with window.

What is the use of window object in JavaScript?

The Window Object It represents the browser's window. All global JavaScript objects, functions, and variables automatically become members of the window object. Global variables are properties of the window object. Global functions are methods of the window object.


2 Answers

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.

like image 118
Blazemonger Avatar answered Sep 19 '22 07:09

Blazemonger


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.

like image 35
J.D. Avatar answered Sep 18 '22 07:09

J.D.