NaN 正负0

typeof NaN // "number"

var a = 3/"bb"; //NaN;
a == NaN; //false what?不应该相等?
a === NaN; //false 还是不想等,这就尴尬了

NaN !== NaN //true
//1.NaN 是 JavaScript 中唯一一个不等于自身的值
// window.isNaN() 检查参数是否不是 NaN,也不是数字 但是这样做的结果并不太准确:
// polyfill

Number.isNaN = Number.isNaN || function(value) {
return typeof value === "number" && isNaN(value);
}
// When the Number.isNaN is called with one argument number, the following steps are taken:

//If Type(number) is not Number, return false.
//If number is NaN, return true.
//Otherwise, return false.
//NOTE This function differs from the global isNaN function (18.2.3) is that it does not convert its argument to a Number before determining whether it is NaN.


//isNaN (number)
//The isNaN function is the %isNaN% intrinsic object. When the isNaN function is called with one argument number, the following steps are taken:

//Let num be ToNumber(number).
//ReturnIfAbrupt(num).
//If num is NaN, return true.
//Otherwise, return false.
//NOTE A reliable way for ECMAScript code to test if a value X is a NaN is an expression of the form X !== X. The result will be true if and only if X is a NaN.

var a = 2 / "foo";
var b = "foo";
a; // NaN
b; "foo"
window.isNaN( a ); // true
window.isNaN( b ); // true——晕!

if(!Number.isNaN){
Number.isNaN = function(n){
return (
typeof n === "number" && window.isNaN(n);
);
}
}
// 利用NaN!==NaN特性判断
if(!Number.isNaN){
Number.isNaN = function(){
return n!==n;
}
}

var a = 0 / -3;
// 至少在某些浏览器的控制台中显示是正确的
a; // -0
// 但是规范定义的返回结果是这样!
a.toString(); // "0"
a + ""; // "0"
String( a ); // "0"
// JSON也如此,很奇怪
JSON.stringify( a ); // "0"
//有意思的是,如果反过来将其从字符串转换为数字,得到的结果是准确的:
+"-0"; // -0
Number( "-0" ); // -0
JSON.parse( "-0" ); // -0

var a = 0;
var b = -0;
a === b //true;
//有些应用程序中的数据需要以级数形式来表示(比如动画帧的移动速度),数字的符号位
//(sign)用来代表其他信息(比如移动的方向)。此时如果一个值为 0 的变量失去了它的符
//号位,它的方向信息就会丢失。所以保留 0 值的符号位可以防止这类情况发生。
//单值(即标量基本类型值, scalar primitive) 总是通过值复制的方式来赋值 / 传递,包括
//null
//、 undefined、字符串、数字、布尔和 ES6 中的 symbol。

posted on 2018-03-20 18:34  taoshengyijiuai  阅读(184)  评论(0编辑  收藏  举报