Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么new Date()是字符串值?_Javascript_Object - Fatal编程技术网

Javascript 为什么new Date()是字符串值?

Javascript 为什么new Date()是字符串值?,javascript,object,Javascript,Object,通常,当我调用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)'} newdate()怎么可能是字符串值?简短回答: 事实并非如此 在控制台中键入: typeof new Date() 正确返回“object” 但是,该对

通常,当我调用
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)'}
newdate()
怎么可能是字符串值?

简短回答:

事实并非如此

在控制台中键入:

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) 
> typeof new Date()
"object"

> typeof "some string"
"string"
编辑:在JavaScript API的一个奇怪的扭曲中,
typeof Date()
确实返回
“string”
。注意缺少的
new
关键字。

简短回答:

事实并非如此

在控制台中键入:

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) 
> typeof new Date()
"object"

> typeof "some string"
"string"
编辑:在JavaScript API的一个奇怪的扭曲中,
typeof Date()
确实返回
“string”
。请注意缺少的
new
关键字。

new Date()
会按预期返回一个对象,而不是字符串

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) 
> typeof new Date()
"object"

> typeof "some string"
"string"
console.log()
必须生成字符串。如果要记录的对象提供了字符串化方法(
toString
),则
console.log()
将使用该方法生成可读的日志条目。

new Date()
会按预期返回对象,而不是字符串

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) 
> typeof new Date()
"object"

> typeof "some string"
"string"

console.log()
必须生成字符串。如果要记录的对象提供了字符串化方法(
toString
),则
console.log()
将使用它生成可读的日志条目。

关于您的代码注释,它是一个对象;)不是一个字符串(注意括号),它只是控制台将其转换为视图。尝试
new Date()instanceof Date
new Date()instanceof String
。toString()隐式获取called@cdhowie愚蠢的JavaScript@Johan当然我只是指出,如果没有相应的
(typeof new Date())===“String”
,ajp15243的
new Date()instanceof String
来覆盖原始大小写,它实际上不是一个100%有用的测试。关于您的代码注释,它是一个对象;)不是一个字符串(注意括号),它只是控制台将其转换为视图。尝试
new Date()instanceof Date
new Date()instanceof String
。toString()隐式获取called@cdhowie愚蠢的JavaScript@Johan当然我只是指出,如果没有相应的
(typeof new Date())==“String”
,ajp15243的
new Date()instanceof String实际上不是100%有用的测试。