I seem to run into a problem with name spacing in JavaScript.
I have separated my javascript objects
into separate files.
Each file starts with namespace definition:
var MySystem = MySystem || {};
If I include the object, which calls some methods of the object that is included in the file after it - I get the TypeError saying that a given object does not exist.
Example of the problem I'm having:
File 1 Url.js (included first in the html document):
var MySystem = MySystem || {};
MySystem.Url = {
objHelper : MySystem.Helper,
init : function() {
"use strict"
if (this.objHelper.isEmpty('string')) {
throw new Error('The string is empty');
}
}
}
File 2 Helper.js (included second in the html document):
var MySystem = MySystem || {};
MySystem.Helper = {
isEmpty : function(thisValue) {
"use strict"
return (
typeof thisValue === 'undefined' ||
thisValue === false ||
thisValue === null ||
thisValue === ''
);
}
}
When called using MySystem.Url.init();
I get:
TypeError: this.objHelper is undefined
if (this.objHelper.isEmpty('string')) {
When I reverse the order of the included files - everything works fine.
This is obviously very simple example, but my system consists of many more objects
- all of them in their own, separate file.
What's the best workaround for this problem?