0

* Disclaimer: I'm not saying this is a good idea - as a matter of fact I'll explicitly say it is not - so take this question by way of trying to understand what exactly the event-loop means for coding style.

My rudimentary understanding of the javascript-has-no-threads mantra is that the runtime treats all of javascript as short "blocks of code" which are scheduled executed one after the other without ever shifting away from a block during execution. A block of code (I don't know the real terminology) in this case is basically code that runs as a result of an event handler being triggered.

If my understanding is correct that would mean that it is technically 100% safe to use global variables if your use of them does not span more than one "block of code".

So for example if I have a single global object window.workspace I could have my event handlers and any code that flows from there - rather than storing temporary variables in closures - store them all in window.workspace. As long as I don't assume that workspace to retain any state in between calls to event handlers (even the same one), this should be perfectly safe.

Is this accurate (though, once again, not advised)

George Mauer
  • 117,483
  • 131
  • 382
  • 612
  • 1
    Well if you don't assume that the global workspace retains any state between events, then what's the point? – Pointy Jul 11 '12 at 17:04
  • @Pointy - no point (though I guess could be useful for debugging?) - just that the answer to the question will help me understand what the event loop actually does – George Mauer Jul 11 '12 at 17:06
  • The whole point of a new scope is to automatically provide this new clean workspace that's perfectly safe to use. If you were to use `window.workspace` and set it to `window.workspace = {}` in every new asynchronous block, then you'd have a relatively safe means of using temporary variables, although you'd be discarding any and all possible optimization, and state preservation associated with new scopes. Woe betide you should you forget to reset `window.workspace`. – zzzzBov Jul 11 '12 at 17:12

1 Answers1

2

Exactly how a JavaScript event mechanism works is up to the container in which JavaScript runs. It would be possible to set up a system wherein event handlers always had some sort of persistent state object passed in on each call.

In browsers and systems like Node.js, however, the answer to your question (to the extent I understand it) is a guarded "yes", or maybe "yes but".

Because JavaScript has closures, a cleaner way to ensure that there's persistent (not like DB persistence; I mean persistent across invocations of an event handler) but private storage is to do something like this:

(function(global) {
  var persistentValue = 12;

  // set up an event handler
  global.whatever().handleEvent(function() {
    if (persistentValue > 12) { ... }
    else { persistentValue ++; }
  });

})( this );

The idea is that the "persistentValue" variable remains "alive" in the closure around the event handler, so each time it's called it'll see that variable as it was the last time it ran. Now, of course if other event handlers are created in the same wrapper function, then they'll also have access to the variable. In that sense, it's like a relatively-global variable to those handlers.

Pointy
  • 405,095
  • 59
  • 585
  • 614