mde / timezone-js

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

No method 'setFromTimeProxy' #101

Closed mnpenner closed 11 years ago

mnpenner commented 11 years ago

When you forget to use the new keyword, you get the following error:

Uncaught TypeError: Object # has no method 'setFromTimeProxy'

This has messed me up a couple of times already because the error message is not very helpful.

Can you do a check to make sure timezoneJS.Date is called with new and if not, either throw a helpful error message, or make it just work?

longlho commented 11 years ago

Calling timezoneJS.Date() without new will leak variables into the global scope and the consequences would be awful. It's the same as when you try to create an instance of an object without new, it gets messy. Not sure if this is a strong use case. @mde any thoughts?

mde commented 11 years ago

Using "new" is a pretty basic concept in JS. I could see throwing an educational error, so people unfamiliar to JS, using it as just a library would learn something. But I sure wouldn't paper over it by making it work without 'new.'

mnpenner commented 11 years ago

Aside from the native Date object, I don't think I've ever really needed to use the new keyword. I imagine not too many people actually know what it does.

It's a one-line fix, isn't it?

if(!(this instanceof timezoneJS.Date)) throw "timezoneJS.Date object must be constructed with 'new'";
mde commented 11 years ago

Send a PR, happy to merge it.

There is a vocal group of people in the JavaScript community who believe fervently in avoiding the use of "new" at all costs, and insist on using other ways of creating objects and reusing functionality, but a cursory survey of mainstream libraries and modules will reveal extensive use of "new" for object-creation, and your standard prototypal inheritance patterns. It's definitely something everybody using JavaScript for anything other than just dropping in a library needs to understand, and understand well.

Another datapoint -- you might be right that a lot of people are trying to figure out JavaScript "new." Attached is Google typeahead results in a Chrome Incognito window. :)

screen shot 2013-08-15 at 8 02 22 pm

mnpenner commented 11 years ago

I've never created a pull request before, nor used Git/GitHub (I develop solo on Hg/BitBucket), so hopefully I did this right. Quite a cool little setup; I can see why people like GitHub.

I tried a few different methods for checking if new was used, this was the only solution I found that worked. I tested in the latest Firefox, hopefully this method works in older browsers. Nothing fancy in there!