通常,当我调用new
关键字时,它绝对会输出一个对象。例如:
function Time(){
this.now = new Date();
}
console.log(new Time());
//{now : 'Tue Aug 26 2014 01:52:15 GMT+0700 (SE Asia Standard Time)'}
new Date()
怎么可能是字符串值?
最佳答案
简答:
不是。
在您的控制台中输入:
typeof new Date()
正确返回 "object"
但是,对象作为名为 toString()
的方法返回,它将返回……一个字符串。
var date = new Date();
console.log(typeof date)
console.log(date.toString())
object
Mon Aug 25 2014 14:58:28 GMT-0400 (Eastern Daylight Time)
编辑:在 JavaScript API 的一个奇怪转折中,typeof Date()
确实返回 "string"
。请注意缺少的 new
关键字。
关于javascript - 为什么 new Date() 是一个字符串值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25492563/