gpt4 book ai didi

javascript - 为什么 (new Date() == new Date()) 为假,而 (Date() == Date()) 为真?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:07:02 27 4
gpt4 key购买 nike

<分区>

我一直在摆弄 JSFiddle 来解决 this FreeCodeCamp 中的问题。当我将 Date 用作字符串时(即没有“new”):

案例一:

function isSameDay (dtFrom, dtTo) {
return dtFrom == dtTo
}

let today = Date()
let tomorrow = Date()

console.log(today)
console.log(tomorrow)
console.log(isSameDay(today, tomorrow))

isSameDay 返回 true。但是,当我将 Date 用作构造函数时(使用“new”):

案例二:

function isSameDay (dtFrom, dtTo) {
return dtFrom == dtTo
}

let today = new Date()
let tomorrow = new Date()

console.log(today)
console.log(tomorrow)

console.log(isSameDay(today, tomorrow))

isSameDay 返回 false。但是(!),当我添加一元运算符“+”时:

案例三:

function isSameDay (dtFrom, dtTo) {
return dtFrom == dtTo
}

let today = + new Date()
let tomorrow = + new Date()

console.log(today)
console.log(tomorrow)

console.log(isSameDay(today, tomorrow))

isSameDay 返回 true。我理解案例 1 和案例 3 返回 true,因为它们只是相同的字符串和相同的毫秒值。

为什么情况 2 返回 false

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