Javascript 字符串上下文中的新日期在Firefox中不起作用
以下代码中的最后一个日志在Firefox中不起作用。为什么?Javascript 字符串上下文中的新日期在Firefox中不起作用,javascript,firefox,node.js,google-chrome,Javascript,Firefox,Node.js,Google Chrome,以下代码中的最后一个日志在Firefox中不起作用。为什么? (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())
(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());
})();
为了在浏览器中测试这一点,我将上面的代码片段放入一个文件中,并使用以下HTML
<!DOCTYPE html>
<body>
<script src="wtf.js"></script>
</body>
铬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}
这在Firefox的String.prototype中似乎没有引用字符串作为字符串。如果向方法中添加:
String.prototype.toDate = function() {
return new Date(String(this));
};
工作正常。很有趣。新建Stringthis不起作用,但Stringthis和this.toString起作用。您还应该能够使用“2012-01-31”。toDate,但我现在无法测试它。
String.prototype.toDate = function() {
return new Date(String(this));
};