'Objects as a interfaces

I've been reading "eloquent javascript" book and I can't get one little thing in the modules chapter.

He (the author) give us this code:

(function(exports) {
  var names = ["Sunday", "Monday", "Tuesday", "Wednesday",
               "Thursday", "Friday", "Saturday"];

  exports.name = function(number) {
    return names[number];
  };
  exports.number = function(name) {
    return names.indexOf(name);
  };
})(this.weekDay = {});

console.log(weekDay.name(weekDay.number("Saturday")));
// → Saturday

But the next thing that he says is :

The previous pattern is commonly used by JavaScript modules intended for the browser. The module will claim a single global variable and wrap its code in a function in order to have its own private namespace. But this pattern still causes problems if multiple modules happen to claim the same name or if you want to load two versions of a module alongside each other.

the question is that I can't understand the pattern problems, anyone could explain me in a more specific way? thanks.



Solution 1:[1]

The code is basically creating a global variable called weekDay (this in the browser will be windows in the outer context where it is being established). So, if another module also tries to create a global called 'weekDay', you will have a conflict. The last one to define it will win and the other definition will be blown away.

If you try to load this module twice (why would you in this case, but in general) then you can't since you can only have a single variable in the global space with that name.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rasmeister