javascript – MDN“Object.is”替代提案

我看过 the MDN page on the “Object.is” method.
它为不提供此方法的浏览器提供了替代代码:
if (!Object.is) {
    Object.is = function(v1,v2) {
        if (v1 === 0 && v2 === 0) {
            return 1 / v1 === 1 / v2;
        }
        if (v1 !== v1) {
            return v2 !== v2;
        }
        return v1 === v2;
    };
}

问题很简单:第二个“如果”何时可以成立?

感谢您的关注.

解决方法

它有点写在 the same article

This is also not the same as being equal according to the ===
operator. The === operator (and the == operator as well) treats the
number values -0 and +0 as equal,and it treats Number.NaN as not
equal to NaN
.

其中的逻辑是楠!==楠在其中!==操作符返回在同一个变量真实,所以它必须是有关NaN的比较的唯一案例.然后,它确实对V2相同的检查,并返回基于结果的假真:如果V2相比是真实的,它是关于NaN的比较为NaN,那么返回true,如果不是则返回false,因为NaN的是从来没有一样东西ISN” t NaN.

相关文章

kindeditor4.x代码高亮功能默认使用的是prettify插件,prett...
这一篇我将介绍如何让kindeditor4.x整合SyntaxHighlighter代...
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面
如何用js控制图片放大缩小