定义一个命名函数,并调用,显示当前是星期几。定义一个匿名函数,并调用,显示当前是星期几。
时间: 2023-12-28 13:04:06 浏览: 39
函数调用显示当前日期
以下是 JavaScript 的代码示例:
命名函数示例:
```javascript
function showDayOfWeek() {
const daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const today = new Date();
const dayOfWeek = daysOfWeek[today.getDay()];
console.log("Today is " + dayOfWeek);
}
showDayOfWeek(); // 调用函数
```
匿名函数示例:
```javascript
const showDayOfWeek = function() {
const daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const today = new Date();
const dayOfWeek = daysOfWeek[today.getDay()];
console.log("Today is " + dayOfWeek);
};
showDayOfWeek(); // 调用函数
```
这两个函数的作用是相同的,都会输出当前是星期几。区别在于,命名函数需要给函数命名,而匿名函数则不需要,直接将函数赋值给一个变量即可。
阅读全文