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

情景一:

function isSame (arg1, arg2) {
  return arg1 == arg2
}

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

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

isSame 返回 true 。
但是,当我使用Date作为构造函数(带有 new)时

情景二:

function isSame (arg1, arg2) {
   return arg1 == arg2
}

let today = new Date()
let tomorrow = new Date()
console.log(isSame(today, tomorrow))

isSame 返回 false 。
但是,当我添加一元运算符 +时

情景三:

function isSame(arg1, arg2) {
    return arg1 ==  arg2
}

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

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

为什么以上几种情况返回的不同呢?

  • 推荐答案

使用 Date(),JavaScript Date对象只能可以通过调用JavaScript Date作为构造函数实例化:作为常规函数调用(即不使用new运算符)将返回字符串,而不是Date对象。

typeof new Date() === 'object'
typeof Date() === 'string'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值