What are we talking about
You can see that the main method that I want to expose to global namespace is
showCalendar. However I am also exposing six other functions to global namespace. It means that if some user has declared a global namespace function named
getCellID then my
getCellID is going to collide with the user’s function. This is not good.
The goal is to refactor the code in such a way that only one method
The above call to
getCellID is currently being executed successfully. It means I have polluted the global namespace. I am going to fix it.
The solution is to put all the code inside a function. In a function all the variables and functions declared are scoped only to that function. Out of that function, inner functions cannot be used. Inside this function only one method
showCalendar would be exposed.
Here is the solution.
In the above case the call to
showCalendar was successful. However call to
getCallID failed. That’s good. It means I am not polluting the global namespace.
If you notice carefully in the above case after declaring an anonymous function I am invoking that function by having
() at the end. You can read more about self-invoking function here .