js中这个地方 这个date不用new就可以?

<!DOCTYPE html>
<html>
<body>

<script>
document.write(Date());
</script>

</body>
</html>

如果date()是一个函数的话,并没有定义呀?在w3cschool上看到的例子。

阅读 3.9k
3 个回答

Date 是内置对象

你用 document.write() 你也没定义为什么可以用呢?

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date

Note: 需要注意的是只能通过调用 Date 构造函数来实例化日期对象:以常规函数调用它(即不加 new 操作符)将会返回一个字符串,而不是一个日期对象。另外,不像其他JavaScript 类型,Date 对象没有字面量格式。

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects

https://developer.mozilla.org/zh-CN/docs/Web/API/Document_Object_Model

Date为JS内置对象

JavaScript Date objects can only be instantiated by calling JavaScript Date as a constructor: calling it as a regular function (i.e. without the new operator) will return a string rather than a Date object; unlike other JavaScript object types, JavaScript Date objects have no literal syntax.

Date();返回当前时间的字符串表示
new Date();返回一个Date对象

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题