为什么 (new Date() == new Date()) 为假,而 (Date() == Date()) 为真?
Why is (new Date() == new Date()) false, but (Date() == Date()) is true?
我一直在摆弄 JSFiddle 来解决 FreeCodeCamp 中的 this 问题。当我将 Date 用作字符串时(即没有 "new"):
案例 1:
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 returns 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 returns false。但是(!),当我添加一元运算符“+”时:
案例 3:
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 returns true。我理解案例 1 和案例 3 return 正确,因为它们只是相同的字符串和相同的毫秒值。
为什么案例 2 return false?
使用 Date()
,JavaScript Date 对象只能通过调用 JavaScript Date 作为构造函数来实例化:将其作为常规函数调用(即没有 new 运算符)将return一个字符串而不是一个日期对象。 MDN Reference.
typeof Date() //"string"
Date() == Date() //true
使用构造函数代替new Date()
,每个实例都是唯一的(同一个构造函数的两个实例仍然不同于each-other),这就是它们比较时不相等的原因.
typeof new Date(); //"object"
new Date() === new Date() //false
简而言之,案例 2 returns 错误,因为 您正在比较两个不同的对象引用(即使两个对象都包含 exact 相同的属性)。
而在其他情况下,您比较的是日期的 toString()
值。
请看官方文档中的注3
NOTE 3
The equality operator is not always transitive. For example,
there might be two distinct String objects, each representing the same
String value.
Each String object would be considered equal to the
String value by the == operator, but the two String objects would not
be equal to each other. For Example:
new String("a") == "a" //true
"a" == new String("a") //true
but
new String("a") == new String("a") //false.
我一直在摆弄 JSFiddle 来解决 FreeCodeCamp 中的 this 问题。当我将 Date 用作字符串时(即没有 "new"):
案例 1:
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 returns 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 returns false。但是(!),当我添加一元运算符“+”时:
案例 3:
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 returns true。我理解案例 1 和案例 3 return 正确,因为它们只是相同的字符串和相同的毫秒值。
为什么案例 2 return false?
使用 Date()
,JavaScript Date 对象只能通过调用 JavaScript Date 作为构造函数来实例化:将其作为常规函数调用(即没有 new 运算符)将return一个字符串而不是一个日期对象。 MDN Reference.
typeof Date() //"string"
Date() == Date() //true
使用构造函数代替new Date()
,每个实例都是唯一的(同一个构造函数的两个实例仍然不同于each-other),这就是它们比较时不相等的原因.
typeof new Date(); //"object"
new Date() === new Date() //false
简而言之,案例 2 returns 错误,因为 您正在比较两个不同的对象引用(即使两个对象都包含 exact 相同的属性)。
而在其他情况下,您比较的是日期的 toString()
值。
NOTE 3
The equality operator is not always transitive. For example, there might be two distinct String objects, each representing the same String value.
Each String object would be considered equal to the String value by the == operator, but the two String objects would not be equal to each other. For Example:
new String("a") == "a" //true "a" == new String("a") //true
but
new String("a") == new String("a") //false.