How do I properly communicate data betweens two scripts.
In this particular case I have an element in one script, but I want to append it in another script.
The simplest way I could do this is by using the global window object as go-between.
But globals are not best practice.
What is the correct way to pass this element to another script?
Both script are encapsulated in the module pattern.
Script 0 Create Element Point
var element = document.createElement( "div" );
element.innerHTML = response_text;
Script 1 Append Element Point
Vi.sta = {
// implemented in the boot loader - shared global space - appended child already - you don't have to append until now.
// Static data has already been composed.
// Pull off window and append or best practice
};
Both are encapsulated in the module pattern
(function(){
// the code here
})()
All JS scripts are run in the global scope. When the files are downloaded to the client, they are parsed in the global scope.
So if you’ve got
There’s nothing stopping you from doing this:
As long as File_1.js is loaded before File_2.js
If you are namespacing, ie:
MYNAMESPACE = {};and each file extends your namespace through modules (rather than “module pattern” referring to returning interfaces from immediately-invoking functions), then check for the module’s existence.If you ARE writing modules for a namespaced app, and you DO need access to variables from one module to another, then you should be providing an
interfacefor that.If you are
SANDBOXINGmodules, then you need to provide them all aproxysimilar to aservice-locatoror a"registry", or develop amessaging-systembased on amediator-pattern.