3.2.2 ES6 數(shù)值分類 ES6 教程數(shù)值的表示二進制表示法新寫法: 前綴 0b 或 0B 。 console.log(0b11 === 3); // true console.log(0B11 === 3); // true 八進制表示法新寫法: 前綴 0o 或 0O 。 console.log(0o11 === 9); // true console.log(0O11 === 9); // true 常量Number.EPSILON Number.EPSILON 屬性表示 1 與大于 1 的最小浮點數(shù)之間的差。 它的值接近于 2.2204460492503130808472633361816E-16,或者 2-52。 測試數(shù)值是否在誤差范圍內(nèi): 0.1 + 0.2 === 0.3; // false // 在誤差范圍內(nèi)即視為相等 equal = (Math.abs(0.1 - 0.3 + 0.2) < Number.EPSILON); // true 屬性特性writable:false enumerable:false configurable:false 最大/最小安全整數(shù)安全整數(shù) 安全整數(shù)表示在 JavaScript 中能夠精確表示的整數(shù),安全整數(shù)的范圍在 2 的 -53 次方到 2 的 53 次方之間(不包括兩個端點),超過這個范圍的整數(shù)無法精確表示。 最大安全整數(shù) 安全整數(shù)范圍的上限,即 2 的 53 次方減 1 。 Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2; // true
Number.MAX_SAFE_INTEGER === Number.MAX_SAFE_INTEGER + 1; // false
Number.MAX_SAFE_INTEGER - 1 === Number.MAX_SAFE_INTEGER - 2; // false
最小安全整數(shù) 安全整數(shù)范圍的下限,即 2 的 53 次方減 1 的負數(shù)。 Number.MIN_SAFE_INTEGER + 1 === Number.MIN_SAFE_INTEGER + 2; // false
Number.MIN_SAFE_INTEGER === Number.MIN_SAFE_INTEGER - 1; // false
Number.MIN_SAFE_INTEGER - 1 === Number.MIN_SAFE_INTEGER - 2; // true
屬性特性 writable:false enumerable:false configurable:false 方法Number 對象新方法 Number.isFinite() 用于檢查一個數(shù)值是否為有限的( finite ),即不是 Infinity console.log( Number.isFinite(1)); // true
console.log( Number.isFinite(0.1)); // true
// NaN 不是有限的
console.log( Number.isFinite(NaN)); // false
console.log( Number.isFinite(Infinity)); // false
console.log( Number.isFinite(-Infinity)); // false
// Number.isFinate 沒有隱式的 Number() 類型轉(zhuǎn)換,所有非數(shù)值都返回 false
console.log( Number.isFinite('foo')); // false
console.log( Number.isFinite('15')); // false
console.log( Number.isFinite(true)); // false
Number.isNaN()
用于檢查一個值是否為 NaN 。
console.log(Number.isNaN(NaN)); // true
console.log(Number.isNaN('true'/0)); // true
// 在全局的 isNaN() 中,以下皆返回 true,因為在判斷前會將非數(shù)值向數(shù)值轉(zhuǎn)換
// 而 Number.isNaN() 不存在隱式的 Number() 類型轉(zhuǎn)換,非 NaN 全部返回 false
Number.isNaN("NaN"); // false
Number.isNaN(undefined); // false
Number.isNaN({}); // false
Number.isNaN("true"); // false
從全局移植到 Number 對象的方法 逐步減少全局方法,用于全局變量的模塊化。 方法的行為沒有發(fā)生改變。 Number.parseInt() 用于將給定字符串轉(zhuǎn)化為指定進制的整數(shù)。 // 不指定進制時默認為 10 進制
Number.parseInt('12.34'); // 12
Number.parseInt(12.34); // 12
// 指定進制
Number.parseInt('0011',2); // 3
// 與全局的 parseInt() 函數(shù)是同一個函數(shù)
Number.parseInt === parseInt; // true
Number.parseFloat()
用于把一個字符串解析成浮點數(shù)。
Number.parseFloat('123.45') // 123.45
Number.parseFloat('123.45abc') // 123.45
// 無法被解析成浮點數(shù),則返回 NaN
Number.parseFloat('abc') // NaN
// 與全局的 parseFloat() 方法是同一個方法
Number.parseFloat === parseFloat // true
Number.isInteger()
用于判斷給定的參數(shù)是否為整數(shù)。
Number.isInteger(value)
Number.isInteger(0); // true
// JavaScript 內(nèi)部,整數(shù)和浮點數(shù)采用的是同樣的儲存方法,因此 1 與 1.0 被視為相同的值
Number.isInteger(1); // true
Number.isInteger(1.0); // true
Number.isInteger(1.1); // false
Number.isInteger(Math.PI); // false
// NaN 和正負 Infinity 不是整數(shù)
Number.isInteger(NaN); // false
Number.isInteger(Infinity); // false
Number.isInteger(-Infinity); // false
Number.isInteger("10"); // false
Number.isInteger(true); // false
Number.isInteger(false); // false
Number.isInteger([1]); // false
// 數(shù)值的精度超過 53 個二進制位時,由于第 54 位及后面的位被丟棄,會產(chǎn)生誤判
Number.isInteger(1.0000000000000001) // true
// 一個數(shù)值的絕對值小于 Number.MIN_VALUE(5E-324),即小于 JavaScript 能夠分辨
// 的最小值,會被自動轉(zhuǎn)為 0,也會產(chǎn)生誤判
Number.isInteger(5E-324); // false
Number.isInteger(5E-325); // true
Number.isSafeInteger()
用于判斷數(shù)值是否在安全范圍內(nèi)。
Number.isSafeInteger(Number.MIN_SAFE_INTEGER - 1); // false
Number.isSafeInteger(Number.MAX_SAFE_INTEGER + 1); // false
Math 對象的擴展ES6 在 Math 對象上新增了 17 個數(shù)學相關(guān)的靜態(tài)方法,這些方法只能在 Math 中調(diào)用。 普通計算 Math.cbrt 用于計算一個數(shù)的立方根。 Math.cbrt(1); // 1
Math.cbrt(0); // 0
Math.cbrt(-1); // -1
// 會對非數(shù)值進行轉(zhuǎn)換
Math.cbrt('1'); // 1
// 非數(shù)值且無法轉(zhuǎn)換為數(shù)值時返回 NaN
Math.cbrt('hhh'); // NaN
Math.imul 兩個數(shù)以 32 位帶符號整數(shù)形式相乘的結(jié)果,返回的也是一個 32 位的帶符號整數(shù)。 // 大多數(shù)情況下,結(jié)果與 a * b 相同
Math.imul(1, 2); // 2
// 用于正確返回大數(shù)乘法結(jié)果中的低位數(shù)值
Math.imul(0x7fffffff, 0x7fffffff); // 1
Math.hypot 用于計算所有參數(shù)的平方和的平方根。 Math.hypot(3, 4); // 5
// 非數(shù)值會先被轉(zhuǎn)換為數(shù)值后進行計算
Math.hypot(1, 2, '3'); // 3.741657386773941
Math.hypot(true); // 1
Math.hypot(false); // 0
// 空值會被轉(zhuǎn)換為 0
Math.hypot(); // 0
Math.hypot([]); // 0
// 參數(shù)為 Infinity 或 -Infinity 返回 Infinity
Math.hypot(Infinity); // Infinity
Math.hypot(-Infinity); // Infinity
// 參數(shù)中存在無法轉(zhuǎn)換為數(shù)值的參數(shù)時返回 NaN
Math.hypot(NaN); // NaN
Math.hypot(3, 4, 'foo'); // NaN
Math.hypot({}); // NaN
Math.clz32 用于返回數(shù)字的32 位無符號整數(shù)形式的前導0的個數(shù)。 Math.clz32(0); // 32
Math.clz32(1); // 31
Math.clz32(0b01000000000100000000000000000000); // 1
// 當參數(shù)為小數(shù)時,只考慮整數(shù)部分
Math.clz32(0.5); // 32
// 對于空值或非數(shù)值,會轉(zhuǎn)化為數(shù)值再進行計算
Math.clz32('1'); // 31
Math.clz32(); // 32
Math.clz32([]); // 32
Math.clz32({}); // 32
Math.clz32(NaN); // 32
Math.clz32(Infinity); // 32
Math.clz32(-Infinity); // 32
Math.clz32(undefined); // 32
Math.clz32('hhh'); // 32
數(shù)字處理Math.trunc 用于返回數(shù)字的整數(shù)部分。 Math.trunc(12.3); // 12
Math.trunc(12); // 12
// 整數(shù)部分為 0 時也會判斷符號
Math.trunc(-0.5); // -0
Math.trunc(0.5); // 0
// Math.trunc 會將非數(shù)值轉(zhuǎn)為數(shù)值再進行處理
Math.trunc("12.3"); // 12
// 空值或無法轉(zhuǎn)化為數(shù)值時時返回 NaN
Math.trunc(); // NaN
Math.trunc(NaN); // NaN
Math.trunc("hhh"); // NaN
Math.trunc("123.2hhh"); // NaN
Math.fround 用于獲取數(shù)字的32位單精度浮點數(shù)形式。 // 對于 2 的 24 次方取負至 2 的 24 次方之間的整數(shù)(不含兩個端點),返回結(jié)果與參數(shù)本身一致
Math.fround(-(2**24)+1); // -16777215
Math.fround(2 ** 24 - 1); // 16777215
// 用于將 64 位雙精度浮點數(shù)轉(zhuǎn)為 32 位單精度浮點數(shù)
Math.fround(1.234) // 1.125
// 當小數(shù)的精度超過 24 個二進制位,會丟失精度
Math.fround(0.3); // 0.30000001192092896
// 參數(shù)為 NaN 或 Infinity 時返回本身
Math.fround(NaN) // NaN
Math.fround(Infinity) // Infinity
// 參數(shù)為其他非數(shù)值類型時會將參數(shù)進行轉(zhuǎn)換
Math.fround('5'); // 5
Math.fround(true); // 1
Math.fround(null); // 0
Math.fround([]); // 0
Math.fround({}); // NaN
判斷Math.sign 判斷數(shù)字的符號(正、負、0)。 Math.sign(1); // 1
Math.sign(-1); // -1
// 參數(shù)為 0 時,不同符號的返回不同
Math.sign(0); // 0
Math.sign(-0); // -0
// 判斷前會對非數(shù)值進行轉(zhuǎn)換
Math.sign('1'); // 1
Math.sign('-1'); // -1
// 參數(shù)為非數(shù)值(無法轉(zhuǎn)換為數(shù)值)時返回 NaN
Math.sign(NaN); // NaN
Math.sign('hhh'); // NaN
對數(shù)方法Math.expm1() 用于計算 e 的 x 次方減 1 的結(jié)果,即 Math.exp(x) - 1 。 Math.expm1(1); // 1.718281828459045
Math.expm1(0); // 0
Math.expm1(-1); // -0.6321205588285577
// 會對非數(shù)值進行轉(zhuǎn)換
Math.expm1('0'); //0
// 參數(shù)不為數(shù)值且無法轉(zhuǎn)換為數(shù)值時返回 NaN
Math.expm1(NaN); // NaN
Math.log1p(x) 用于計算1 + x 的自然對數(shù),即 Math.log(1 + x) 。 Math.log1p(1); // 0.6931471805599453
Math.log1p(0); // 0
Math.log1p(-1); // -Infinity
// 參數(shù)小于 -1 時返回 NaN
Math.log1p(-2); // NaN
Math.log10(x) 用于計算以 10 為底的 x 的對數(shù)。 Math.log10(1); // 0
// 計算前對非數(shù)值進行轉(zhuǎn)換
Math.log10('1'); // 0
// 參數(shù)為0時返回 -Infinity
Math.log10(0); // -Infinity
// 參數(shù)小于0或參數(shù)不為數(shù)值(且無法轉(zhuǎn)換為數(shù)值)時返回 NaN
Math.log10(-1); // NaN
Math.log2() 用于計算 2 為底的 x 的對數(shù)。 Math.log2(1); // 0
// 計算前對非數(shù)值進行轉(zhuǎn)換
Math.log2('1'); // 0
// 參數(shù)為0時返回 -Infinity
Math.log2(0); // -Infinity
// 參數(shù)小于0或參數(shù)不為數(shù)值(且無法轉(zhuǎn)換為數(shù)值)時返回 NaN
Math.log2(-1); // NaN
雙曲函數(shù)方法
指數(shù)運算符1 ** 2; // 1
// 右結(jié)合,從右至左計算
2 ** 2 ** 3; // 256
// **=
let exam = 2;
exam ** = 2; // 4 |
|