(A more simplified example would be how in school classroom, perhaps one might find two students have the same first name. How does one distinguish one from the other if the teacher wishes to acknowledge only one of them? How will each student know who is being called on? They can’t. The same problem can happen with programming.)
Functions Signify Scope (aka “my home is your home”)
Imagine that you have just moved into a new house and this weird outgoing person suddenly spots you in the front yard, introducing herself as your neighbor. She gives you massively big hug, drags you into her house by pulling your arm, forcing you have a cup of her coffee and have some of her cake. She goes on to assure you, “If you need anything at all, just come by – any time! My home is your home.” You go on to nod and smile and wonder how anyone can be so open to visitors seeing you just met them. Oh well, they seem nice enough.
This is how JS also operates…
Ultimately, “this” (in programming) literally points one level up from where you have your current line of programming code. Applying this reasoning, everything one level up from any function used in an HTML page, will incidentally automatically be the window object where the HTML resides. And yes, that means in a way, that the whole thing is one giant object! So to go one level up from a variable in a function can in theory mean you are in the global scope of that one big “function” (think “also object” – they are the same thing in this case) is therefore accessible EVERYWHERE – even in other functions you might choose make. They really are just like very close neighbors – perhaps even uncomfortably close.
How This Relates to Namespacing
Going back to the neighbor analogy, it is best not to shrug off the neighbor as merely being just nice enough. Who knows, she might start even barging into your home next time with the same degree of openness under the guise of friendship:
Your home is their home and their home is your home! Things also get complicated when sharing happens. In a work environment, people will of course almost always work together to code. They will share code to break down the workload and break down complex tasks into simpler ones:
“In any modern day application, Namespace is a must to have because at the end, we need third party libraries and components.” – Code Project
Namespacing in Action
Imagine you have a main file called index.html:
Imagine you have a file called announcement1.js
Imagine you have another file called announcement2.js
Imagine you have a third file that calls for all the action
RUNNING THE PROGRAM
PROBLEM! This will not work as expected! Instead of displaying “President” on one line and “VicePresident” on another, it will print the second variable twice. While the functions have different names the variable name was the same (in the global scope) and was overridden.
HOW TO FIX!
Create separate objects with properties as a work-around. Change announcement1.js to be as follows:
Change announcement2.js to be as follows:
Change the third file that calls for all the action
This should now work as expected! It will print:
All things mean much more when applied in an actual setting where you can see the code in action. Here is some example code from CodeProject before it was made namespace-friendly:
It looks like your typical program, right? And here is that same code made so that it is namespaced:
Can you see the difference? The sections of code are now lumped together as one big chunk now: a global object called MYAPPLICATION and “To access any of the methods or variables, you need to fetch it through the MYAPPLICATION.”
It clearly takes some more work to make something namespace-friendly, but as Code Project points out, these three global items, calculateTax, product, doTaxCalculations “will really put us in trouble if we are using 3rd party libraries and they also contain the same name. Like if it is used before other library, it will be overridden by Library and vice versa.” In this case, record keeping and handling data for sales are involved, and one cannot help but imagine can potentially create serious problems if the money matters are not computed correctly.
(Screenshot taken from thanpolas post entitled, Development Using Namespaces)