javascript - Using unary plus (+) to compare Dates for equality
问题描述
The most standard way in javascript way to compare dates that I've seen is
let a=new Date();
let b=new Date(a);
let equals = a.getTime()==b.getTime(); //true
/* a==b is false */
but
let equals = +a == +b
works well, looks quite clear and saves typing. I haven't seen many examples of that way to compare dates, almost all use getTime().
Is there any drawback or problem with the unary + applied this way?
解决方案
客观上,不存在语用问题。一元+
触发(通过抽象的 ToNumber 和 ToPrimitive 操作)上的操作,它返回相同valueOf
的值。所以你得到相同的结果。Date
Date
getTime
推荐阅读
- python - 熊猫年龄分组
- java - Java Node 数组节点突变错误 - 空指针
- laravel - 如何在 Laravel 6/7 中访问多个多对多关系
- r - R中的矩阵重采样循环
- python-3.x - PyQt5 QTextEdit changes color when copy / pasting into it
- python-3.x - 如何在 python docker 容器中侦听 HTTP ping?
- node.js - 如何将文件上传到 dockerized Node.js 容器(服务器)?
- python - 数据框将两个元素与函数进行比较
- groupme - 返回的名称和昵称对于组成员来说是相同的
- java - 是什么让 Spring Boot 控制台变得丰富多彩?