gpt4 book ai didi

javascript - 无法打印 getDay() 方法的结果

转载 作者:行者123 更新时间:2023-12-01 01:23:43 26 4
gpt4 key购买 nike

我刚刚开始学习 Javascript,所以我试图应用我学到的第一件事来锻炼一下。首先,我尝试创建一个 .html 文件,然后将其链接到外部 .js 文件并写入一些行。

更具体地说,我尝试在页面上打印将 getDay() 方法应用于今天的日期的结果。我是这样写的:

const now = Date();

const today = now.getDay();

document.write(today);
<html>
<head>
<link href="./style.css" type="text/css" rel="stylesheet">
</head>
<body>
<script src="script.js" type=text/javascript></script>
</body>
</html>

有人可以告诉我为什么我的 getDay() 方法不起作用吗?这不是链接的问题,因为编写其他内容也可以。

我不是在寻找解决方法,只是在寻找解释以了解我做错了什么。

提前非常感谢!

最佳答案

您必须使用new operator创建 Date 对象的实例:

The new operator creates an instance of a user-defined object type or of one of the built-in object types that has a constructor function.

const now = new Date();

const today = now.getDay();

document.write(today);

关于javascript - 无法打印 getDay() 方法的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54007787/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com