The last log in the following code doesn’t work in Firefox. Why?
(function() {
String.prototype.toDate = function() {
return new Date(this);
};
console.log(Date.parse("2012-01-31"));
console.log(new Date("2012-01-31"));
console.log("2012-01-31".toDate());
})();
To test this in a browser, I put the snippet above into a file and used the following HTML.
<!DOCTYPE html>
<body>
<script src="wtf.js"></script>
</body>
NodeJS (v0.4.12):
1327932000000
Mon, 30 Jan 2012 14:00:00 GMT
Mon, 30 Jan 2012 14:00:00 GMT
Chrome (17.0.963.79):
1327968000000
Tue Jan 31 2012 10:00:00 GMT+1000 (EST)
Tue Jan 31 2012 10:00:00 GMT+1000 (EST)
Firefox (10.0):
1327968000000
Date {Tue Jan 31 2012 10:00:00 GMT+1000 (EST)}
Date {Invalid Date}
thisin Firefox’s String.prototype appears to not reference the string, as a string. If you add to your method:it works alright.