First check Why are function declarations handled differently in different browsers?
This seems like a hoisting issue. The if
statement gets ignored because this is what happens:
if (typeof RegionUpdater === 'undefined') {
function RegionUpdater() {}
}
The above will be interpreted in the following because functions get hoisted (moved to) the top of the closest scope (function), in this case the scope is the global object.
function RegionUpdater() {}
if (typeof RegionUpdater === 'undefined') {
// won't run since RegionUpdater is already defined
}
You could workaround it like this:
var RegionUpdater = RegionUpdater || function RegionUpdater(){
};
Edit: Like Mics said, using a function expression should work:
if (typeof RegionUpdater === 'undefined') {
var RegionUpdater = function RegionUpdater(){};
...
}
Because this is how it happens:
var RegionUpdater;
if (typeof RegionUpdater === 'undefined') {
RegionUpdater = function RegionUpdater(){};
...
}