No method 'setFromTimeProxy'
mnpenner opened this issue · 5 comments
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 withnew
and if not, either throw a helpful error message, or make it just work?
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?
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.'
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'";
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. :)
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!