简体   繁体   English

2个未定义变量的结果类型

[英]typeof result of 2 undefined variable

I just test the "typeof" in javascript, and really don't know why the result is like this. 我只是在javascript中测试“ typeof”,所以实际上不知道为什么结果是这样的。

/ * ** * / / * ** * /

var cota,
    plouto;

alert(typeof plouto/cota); //NaN 

/ * ** * / / * ** * /

var cota,
    plouto;

alert(typeof (plouto/cota)); //number

/ * ** * / / * ** * /

var cota,
    plouto;

var flo = plouto/cota;

alert(typeof flo); //number 

The first one alerts NaN because the typeof plouto is executed first, and the result is divided by cota . 第一个警告NaN因为首先执行typeof plouto ,结果除以cota The result of that is not a number, hence NaN . 其结果不是数字,因此不是NaN You could imagine it like this: 您可以这样想象:

(typeof plouto) / cota

The second one divides plouto by cota , which is not a number (because both variables are undefined ), but the type of NaN is actually Number , which can be confusing! 第二个分ploutocota ,这不是一个数字(因为这两个变量是undefined ),但类型NaN实际上是Number ,这可能会造成混淆!

The same goes for the third example. 第三个示例也是如此。

The typeof operator has precedence over the math operators, so it's executed first. typeof运算符的优先级高于数学运算符,因此首先执行它。

What you get from typeof plouto/cota is first typeof plouto then the return value divided with cota - resulting in Not A Number. 你从得到typeof plouto/cota是第一typeof plouto然后分返回值cota -导致不是一个数字。 When doing math operation and one of the "participants" is not a number, the whole result will also be NaN. 当进行数学运算并且“参与者”之一不是数字时,整个结果还将为NaN。

The other two cases are more simple: the type of NaN is number. 其他两种情况更简单:NaN的类型是数字。 Think of that as the equivalent of null for objects. 可以认为这与对象的null等效。

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

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