You are defining an object literal to be used as namespaces. Sometimes you want to group your functions and objects in logical groups like an url but in reverse. For example:
com.myCompany.myApplication.Dom
com.myCompany.myApplication.Validators
Where Dom does dom stuff and Validators do validation. You can’t just define
com.myCompany.myApplication.Dom
because window.com is undefined and you can’t add myCompany on window, this is why in your code window.COB = window.COB || {};
checks if COB exist on window and if it doesn’t it’s created.
Later you can organise your code in several files so Dom will go in Dom.js (like in Java). If I want to create com.myCompany.myApplication.Dom I have to check step by step if the object hasn’t already been created:
com=com||{}; //if com doesn't exist create it
com.myCompany=com.myCompany||{};//if com.myCompany doesn't exist create it
//... and so on
This is because com.myCompany might already be declared in the Validators.js or any other js file and you don’t want to just overwrite it.
The code:
window.COB.AppHelper = {
getUrl: function (absoluteUrl) { ... },
};
will throw an error because COB is undefined on window, you have to define it first.
6
solved understanding JavaScript classes and objects [closed]