1

Good day everyone,

i have a question which i wasn't able to solve via google (maybe i don't know the right search term) so if my question is already answered i apologize.

I try to learn some JavaScript and read about global variables belonging to the window object on w3schools which may have led to a misunderstanding on my side.

Let's say i have two scripts:

  • script1.js
  • script2.js

which i load into a html-document in the head section. Both contain something like

var globy = 0;

(and/or possible manipulations of globy)

Now the question is: Does the belonging to the window-object of "globy" mean that both scripts share and manipulate the same "globy" ?

If this is not the case, another question results: is it possible to achieve such a thing like a global variable with a scope spanning multiple *.js files?

I am wondering because i was thinking about advantages and disadvantages that would bring like easy communication between scripts on the +side and easy unwanted interference on the -side.

I would appreciate correction/verification. Greetings, Wolf

Wolfone
  • 1,276
  • 3
  • 11
  • 31
  • 3
    Yes, `window` properties are global to the page, not the script file. – Pointy Aug 30 '16 at 12:10
  • 2
    Yes, they interfere, and about the communication, there are better solutions, more reliable solutions. But that depends on your scripts. – Thomas Aug 30 '16 at 12:12
  • thanks you two, that led me to some interesting search ideas! – Wolfone Aug 30 '16 at 12:16
  • I stuck with the same topic. I have two scripts with the same logic but different variables generated dynamically (im building third party js widget). Everything was working untill there was only one script, loading one widget and data through xhr CORS. When two script start to work instantly i have problems with object properties. It seems (through console.log) that first script is running first and ends, than the second script runs byt the first script objects have already the data from second script, that logs indicate that it runs after the first one! :D Any help or some hints? – Gawrion Apr 18 '23 at 18:10
  • It's difficult to answer correctly without details on your case but generally speaking today I would rather advocate to choose a design that makes the correct working of your code independent from script-load-order. Depending on what you do, you might be able to achieve that using Events or CustomEvents. Other keywords you might wanna look into are "dynamic imports" and "promises". – Wolfone Apr 19 '23 at 07:16
  • Hey! I interpreted wrong console.log objects because when objects revealed they updated last execution state, which was missleading. I also managed to build race condition preventing system. Its based on loging data and events into objects and arrays, comparing hashes of updated divs, and comparing recieved xhr responses and it's hashes to updated div's hashes, so no matter which xhr response come i'm able to manage the situation :D 48 hours of head scratching, reading and trying different approaches. Priceless? My new knowledge :D – Gawrion Apr 19 '23 at 07:56
  • Oh, i see! That's a logging behaviour that has thrown me off quite a bunch of times as well. Great you could solve your issue! If you have to deal with possible race conditions a lot, looking into Promises might be a way to do things if you want to get some grip on the situation. Furthermore as a general addition to your thoughts this particular answer to another question might be a good source of information: https://stackoverflow.com/a/22153067/6294605 Especially as it also elaborates on the difference between "async" and "concurrent". – Wolfone Apr 19 '23 at 09:58

0 Answers0