New static properties
ES6 adds some static constant attributes to the Number object to aid calculations.
Number.EPSILON
Number.EPSILON
( 2 -52 ) represents the smallest difference between any two values. In other words, if the absolute value of the difference between a and b is less thanNumber.EPSILON
, then we can consider these two values to be equal of. which is:$$ |a-b|<Number.EPSILON \ \ \} \Longrightarrow a=b $$
In JavaScript, integers are not a separate type. Something different from what we imagined is that all the numbers JS are double-precision floating-point numbers . But floating-point operations only produce approximate results, rounded to the nearest integer, so sometimes, operations accuracy would be a problem.
console.log(0.1 + 0.2); //0.30000000000000004 console.log(0.1 + 0.2 === 0.3);//false
In this case, we can directly use the constant Number.EPSILON
for accuracy judgment:
function isEqual(a, b) {
return Math.abs(a - b) < Number.EPSILON;
}
console.log(isEqual(0.1 + 0.2, 0.3));//true
Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER
These two constants
Number.MAX_SAFE_INTEGER
( 2 53 is -1 ),Number.MIN_SAFE_INTEGER
( -2 53 is + 1'd ) and their name suggests, they are capable of representing the JS safe handling maximum and minimum integer integers .As mentioned earlier, the numbers in JS are double-precision floating-point numbers, and double-precision floating-point numbers can represent up to 53-bit precision integers. The integer range it represents is [ -2 53 +1 , 2 53 -1 ] . When an integer exceeds this range, JS can no longer " safe " handle the integer, here " safe " means that JS can and can correctly compare the integer .
console.log(2 ** 53 - 1 === Number.MAX_SAFE_INTEGER); console.log(-(2 ** 53 - 1) === Number.MIN_SAFE_INTEGER); //显然,a+1===a+2,是不符合我们的预期的。 console.log(Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2); //true
New static function
Number.isNaN
Function type:
//接收一个任意类型的可选参数,返回一个布尔值结果 (v?:any)=>boolean
Number.isNaN
is used to solve the defects of the window.isNaN
In window.isNaN
, the incoming non- number
type parameters number
type, and then judged, which leads to some strange results in our opinion:
//'str'转成number类型之后得到 NaN
console.log(isNaN('str')); //true
//''转成number类型之后得到0
console.log(isNaN('')); //false
Number.isNaN
fixes this behavior and will not convert the parameter to number
, so its execution result may be more in line with our expectations.
console.log(Number.isNaN('str')); //false
console.log(Number.isNaN('')); //false
Number.isFinite
Function type:
//接收一个任意类型的可选参数,返回一个布尔值结果 (v?:any)=>boolean
Number.isFinite
used to determine whether the passed parameter is a finite value. It is the global window.isFinite
relationship can be compared Number.isNaN
function and isNaN
relations: global isFinite
function parameters will be converted to number
, and Number.isFinite
not to do so.
Basically, Number.isFinite(a) === (typeof a=== 'number' && isFinite(a))
console.log(Number.isFinite(42)); //true
console.log(isFinite(42)); //true
console.log(Number.isFinite('42')); //false
console.log(isFinite('42')); //true
Number.isInteger
Function type:
//接收一个任意类型的可选参数,返回一个布尔值结果 (v?:any)=>boolean
Because the numbers in JS are double-precision floating-point numbers, there is no way to determine whether a number is an integer by type. ES6's new Number.isInteger
is used to solve this problem-an auxiliary function to determine whether a value is an integer.
console.log(Number.isInteger('42')); //false
console.log(Number.isInteger(42.0)); //true
console.log(Number.isInteger(42.1)); //false
Number.isSafeInteger
Number.isSafeInteger
andNumber.isInteger
compared to more than a defined range, which is an integer ranging safely [ Number.MIN_SAFE_INTEGER , Number.MAX_SAFE_INTEGER ] .
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。