In this article, I will explain the details of JavaScript namespaces, which should be known to SharePoint/ Office 365 developers who use JavaScript Object Model(JSOM) for SharePoint / Office 365 development.
Background
I have written one article regarding JavaScript variables
In this article, we will discuss JavaScript namespaces. I feel, it’s really very important to understand the concept of namespaces while working on JSOM or SharePoint hosted apps.
Global namespace
- By default, JavaScript doesn’t provide namespace.
- Anything that we write in JavaScript file is placed into Global namespace by default includes all variables and functions.
- In the Browser, Global namespace container is “Window” object.
- Issue with directly, using Global namespaces- Whatever we write to JavaScript the file goes to Global namespaces, there might be the chances that naming conflicts appear. For example, if on my site, if we are loading a couple of JavaScript files, there might be the chance that the same function name or same variable name is there in the different files, which will cause the conflict and the code will not behave correctly. Solution to this problem is using namespace in our JavaScript code..
Custom namespace
- To address issue with Global namespace, as mentioned above in point 4 under Global namespace section, we can create our own Custom namespaces in JavaScript.
- Custom namespace is nothing but the object defined within Global namespace.
- Defining Custom namespace.
-
- var Validations = window.Validations || {};
Here, we are creating Custom namespace “Validations” under Global namespace object.
- We can add our methods/functions, followed by namespace Validations given below.
- Validations.validateEmpty = function() {
-
- }
Here, we have created a method called “validateEmpty()” under namespace “Validations”. Even though if same method is in another JS file on the same page, then also the conflict will not occur because our method is isolated by our Custom namespace.
While calling validateEmpty() method, we need to call it by using namespace like Validations.validateEmpty().
- Similarly, we can create our variables in our Custom namespace, as shown below.
-
- var Constants = window.Constants || {};
Now, we can create variables under our custom Constants namespace, as shown below
- Constants.cssFileURL = “My CSS File URL”;
This is a very common scenario, where we can have all our validations methods in “Validations” namespace, all constants in “Constants” namespace and all SharePoint list CRUD operations in “SharePointCRUD” namespace. In this way, our code will be isolated and there will be no chances of code conflicts.
Advantages of using namespaces
- Code Isolation
No code conflicts with other code on the page in case multiple JavaScript files are loaded or any third part JavaScript files arw referred. In SharePoint Online, SharePoint itself loads its JavaScript files, so there are high chances of conflicts. The better solution is to use our Custom namespaces.
- Clean code improves the code readability. Code maintenance is easy.
Thanks. Any comment/ suggestions/ feedback/ questions are always welcome.