I currently have a collection of files like:
- library0.js
- library1.js
- ...
- libraryn.js
Each file contributes to the creation of a global object known as "MY_GLOBAL" similarly to this example:
library0.js
// Ensure the MY_GLOBAL namespace is available
if (typeof(MY_GLOBAL) === 'undefined') {
var MY_GLOBAL = {};
}
MY_GLOBAL.TIMEOUT = 120000; // Represents 2 minutes
MY_GLOBAL.extractErrMsg = function(reqResponse) {
console.log(reqResponse);
};
library1.js
// Ensure the MY_GLOBAL namespace is available
if (typeof(MY_GLOBAL) === 'undefined') {
var MY_GLOBAL = {};
}
MY_GLOBAL.STRING = 'STRING';
MY_GLOBAL.exposed = function() {
console.log("This is exposed");
};
In my current index.html file, I include all these files as script includes. As a result, any other JavaScript files can easily access:
MY_GLOBAL.extractErrMsg
or any other function/object within the MY_GLOBAL namespace.
I am in the process of transitioning to TypeScript and grappling with how to handle this global namespacing issue. Whenever I refer to the global object MY_GLOBAL, I encounter an error message TS2304: Cannot find name 'MY_GLOBAL'. Should I convert all of these to modules?
Appreciate your insights!