GithubHelp home page GithubHelp logo

精读《null >= 0?》 about weekly HOT 5 CLOSED

ascoders avatar ascoders commented on April 29, 2024
精读《null >= 0?》

from weekly.

Comments (5)

LeeYunhang avatar LeeYunhang commented on April 29, 2024 5

@ascoders 说的不错,但是有点问题,[] 是转换成 "" 而不是 "0";对于允许隐式转换的比较操作,只要一个操作数是 NaN,那么这个表达式一定返回 false。

这是隐式转换表

from weekly.

ascoders avatar ascoders commented on April 29, 2024 4

当比较两边类型不同时,先做类型转换,当类型相同时,如果是 >= 判断,会转成 < 判断。而 NaN 会无视类型转换直接给出 false 结果,因此很可能跳过了 < 判断。

from weekly.

linhuiw avatar linhuiw commented on April 29, 2024

按文中说的,

if null < 0 is false, then null >= 0 is true; 

那可以理解 为什么 null >= 0 了.
同理 {} >= {} 也是 true;

{} < {} // false
{} >= {} // true

可是那为啥

null == {} // false
null < {} // false
null >= {} //false

image

from weekly.

ascoders avatar ascoders commented on April 29, 2024

@linhuiw js 作判断时,首先在两个值上调用 ToPrimitive coercion,如果两个调用的结果都不是 string,就会对两个值进行 ToNumber 强转为 number,然后进行数值比较。

if null < 0 is false, then null >= 0 is true;

null >= 0 // is null < 0 ?
+null < 0 // => 0 < 0 => false, so null >= 0 is true

让我们看一下为什么 null >= [] === true?

null >= [] // is null < []? => 将右边转换为 0 => 将 null 转换为 0 => 0 < 0 显然是 false
// so null >= [] === true

Tip: 感谢 @mrcooder 的提示,此处答案已修正

再看看 null >= [1] 呢?答案是 false,最终会进行 0 < 1 的比较,因此反过来就是 false。

至于下面的情况:

null >= {} is false;

[] '' '0' false 强转 ToNumber 后值为 0,而 Number({}) 的结果为 NaN

null >= {} // is null < {} => is null < NaN => is 0 < NaN === false
// so null >= {} === true?

正常来看 null >= {} 结果应该是 true,但结果却是 false。我估计因为 NaN 在 js 中是个特殊的存在,所以直接判定为 false,而没有进行反转判断。

from weekly.

realwugang avatar realwugang commented on April 29, 2024

学习了。

from weekly.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.