新聞中心
ECMAScript 6.0(以下簡(jiǎn)稱 ES6)是 JavaScript 語(yǔ)言的下一代標(biāo)準(zhǔn),已經(jīng)在 2015 年 6 月正式發(fā)布了。它的目標(biāo),是使得 JavaScript 語(yǔ)言可以用來(lái)編寫復(fù)雜的大型應(yīng)用程序,成為企業(yè)級(jí)開發(fā)語(yǔ)言。

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


咨詢
建站咨詢
