mde / timezone-js

DEPRECATED: Timezone-enabled JavaScript Date object. Uses Olson zoneinfo files for timezone data.
824 stars 183 forks source link

greater flexibility and protection when exporting timezoneJS #106

Closed putermancer closed 11 years ago

putermancer commented 11 years ago

The current method for exporting doesn't play well with some other less-friendly libraries. This can cause things like infinite recursion and all sorts of pain.

Specific library causing me problems: https://github.com/bytespider/jsOAuth jsOAuth does this:

var exports = exports || this;
exports.OAuth = (function (global) {
...

In the browser, the problem here is that exports is now window. timezoneJS then comes along and does this:

timezoneJS = exports; // equivalent to `timezoneJS = window;`
...
timezoneJS.Date = ...; // equivalent to `window.Date = ...;`

Then, any time you try to create a timezoneJS.Date object it invokes new Date(...) which is equivalent to calling timezoneJS.Date(...) -- you see the problem this creates.

So I adapted the export pattern used by timezoneJS to follow the pattern used by underscore.js which also accounts for module and the problems went away. The tests all still pass for me in node@v0.10.12 and it also works in harmony with jsOAuth and other modules that blindly muck around with the global exports object.