简体   繁体   English

有人可以向我解释这种JavaScript行为吗? isNaN([])

[英]Can someone explain me this javascript behaviour? isNaN([])

I just don't get it: 我只是不明白:

isNaN([]) === false -> true

In the browser or in node.js 在浏览器或node.js中

I'd like to understand why. 我想知道为什么。

this has to do with type coercion. 这与强制类型有关。 when isNaN receives anything but a number, it coerces the value to a number... so it basically does +value . isNaN收到除数字以外的任何内容时,它会将值强制转换为数字...因此它基本上执行+value since +[] is 0 and 0 is a number, isNaN returns false . 由于+[]00为数字,所以isNaN返回false

If you want to check whether a value is not a number, you need to convert it with parseInt (or parseFloat ). 如果要检查值是否不是数字,则需要使用parseInt (或parseFloat )对其进行转换。 both those functions will return NaN for an empty array. 这两个函数都将为一个空数组返回NaN。

The difference is with conversion you're explicitly telling it the input is number-like and should therefore be able to be converted into a value of type Number without issues. 区别在于转换,您明确地告诉它输入是数字类型的,因此应该可以将其转换为Number类型的值而不会出现问题。 Since [] is not number-like, trying to parse (convert) it will result in NaN. 由于[]不是数字,尝试解析(转换)将导致NaN。

This is an extremely simplified explanation of how parsing and conversion work. 这是对解析和转换如何工作的极为简化的解释。 For one, they are two separate things. 首先,它们是两件事。 if you want to learn more about how these two work, start here 如果您想了解有关这两种方式的更多信息,请从这里开始

With coercion, however (which is what you're doing), you essentially passed it a value and "assumed" (for lack of a better word) the values type is compatible with the parameters' requirements. 但是,使用强制(这就是您正在执行的操作),实际上是向它传递了一个值,并且“假定”(因为缺少更好的词)值类型与参数的要求兼容。 So isNaN receives an array and guesses you meant to pass it a number, and coerces it to its number equivalent (in the case of an empty array, 0) before performing the actual operation. 因此,isNaN会收到一个数组,并猜测您要向其传递一个数字,并在执行实际操作之前将其强制转换为与它相等的数字(如果是空数组,则为0)。 so isNaN is performed on 0, not an empty array. 所以isNaN是在0而非空数组上执行的。

Again, this explanation is oversimplified. 同样,这种解释过于简单。 for more information about type coercion in JS, go here 有关JS中的类型强制的更多信息,请转到此处

NAN is what is returned when a math function fails. NAN是数学函数失败时返回的内容。 NaN is present in almost all languages that do floating point math. NaN几乎存在于所有进行浮点运算的语言中。 However some Javascript functions (and the internal logic that coerces values) also return NaN when values are coerced to being a number that do not have a numeric representation. 但是,当将值强制转换为没有数字表示形式的数字时,某些Javascript函数(以及强制转换值的内部逻辑)也会返回NaN。 It's a lot easier to understand why isNaN is defined the way it is if you think about NaN as being intended for the case where your math gave a nonsense answer and ignore its role in representing numeric coersion failures. 如果您认为NaN打算用于数学给出了无意义的答案而忽略了它在表示数字强制失败中的作用的情况,那么就容易理解isNaN的定义方式。 Obviously when writing Javascript, you'll need to consider (and use) the coersion NaN, but this particular language design choice makes more sense if you ignore that for the moment. 显然,在编写Javascript时,您需要考虑(并使用)强制NaN,但是如果您暂时忽略这种特定的语言设计选择,则更有意义。

So, for example math.sqrt(-1) is NAN. 因此,例如math.sqrt(-1)是NAN。 the isNAN function returns true if and only if its input argument is NAN. 当且仅当其输入参数为NAN时, isNAN函数才返回true。 Since [] is not NAN, I'd expect isNAN([]) to return false. 由于[]不是NAN,所以我希望isNAN([])返回false。 isNAN is the wrong function for determining if something is a number. isNAN是确定某物是否为数字的错误函数。 It does have strange coersion rules, buteven without them, the answer would be the same in the case of isNAN([] . [] is not the NAN object, so isNAN([]) is false. As @Nicolás Straub points out, the implementation happens to coerce [] to a number first, but we'd expect the same result given the interface contract of isNAN even if it did not. 它确实具有奇怪的强制规则,但是即使没有规则,在isNAN([]的情况下答案也将是相同的。 []不是NAN对象,所以isNAN([])是假的。正如@ isNAN([])指出的那样,该实现碰巧首先将[]强制为一个数字,但是考虑到isNAN的接口协定,即使没有,我们也希望得到相同的结果。

ReferenceError: ISNAN is not defined
at repl:1:1
at REPLServer.defaultEval (repl.js:272:27)
at bound (domain.js:287:14)
at REPLServer.runBound [as eval] (domain.js:300:12)
at REPLServer.<anonymous> (repl.js:441:12)
at emitOne (events.js:82:20)
at REPLServer.emit (events.js:169:7)
at REPLServer.Interface._onLine (readline.js:203:10)
at REPLServer.Interface._line (readline.js:542:8)
at REPLServer.Interface._ttyWrite (readline.js:819:14)

isNaN [Function: isNaN] isNaN [功能:isNaN]

isNaN("bar") true isNaN(“ bar”)true

However, "bar" is not actually the IEEE not a number floating point construct, so we'd expect that a function would tell us "bar" is not NaN . 但是, "bar"实际上不是IEEE而不是数字浮点构造,因此我们希望函数会告诉我们"bar"不是NaN Because isNaN coerces its argument to a number, it gives us the wrong answer. 因为isNaN其参数isNaN为数字,所以它给了我们错误的答案。 The es6 Number.isNaN function would give us the correct answer "bar" is not NaN . es6 Number.isNaN函数将为我们提供正确的答案, "bar"不是NaN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM