[英]Can anyone tell me how this javaScript function works please?
I am a beginner and recently I have stumbled onto this.我是一个初学者,最近我偶然发现了这个。 I do not understand what this
d.getDays()
does.我不明白这个
d.getDays()
做了什么。 Please help me out.请帮帮我。
const dateBuilder = (d) => { let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] let day = days[d.getDay()]; let date = d.getDate(); let month = months[d.getMonth()]; let year = d.getFullYear(); return `${day} ${date} ${month} ${year}` } console.log(dateBuilder(new Date()));
It's a method of the Date
object that returns a number representing the day of the week (Monday, Tuesday etc.) that correlates to the days
array you have in your code.它是
Date
object 的一种方法,它返回一个代表星期几(星期一、星期二等)的数字,该数字与代码中的days
数组相关。 Read more here . 在这里阅读更多。 It's basically getting the "name" of the day by using that array
days
:它基本上是通过使用该数组
days
获得当天的“名称”:
let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.