本文介紹了ES6中全新的數字方法(ECMAScript 6)。
本文將向您介紹添加Number
數據類型的新方法和常量。當然,這里采用的方法并不完全是全新的,但它們已經可以在 and/or 直接移動(例如isNaN())。我們會通過一些例子進行實踐。
我要介紹的第一種方法是Number.isInteger()
。它是JavaScript的新增功能,您之前可能已經定義和使用過這個方法。它確定傳遞給函數的值是否為整數。如果函數值是true,則返回此方法,false則跳出。這種方法的實現非常簡單,并且是原生JavaScript語法。重寫此功能的方法之一是:
Number.isInteger = Number.isInteger || function (number) { return typeof number === 'number' && number % 1 === 0; };
僅僅為了好玩,我重新改寫了這個功能,采用了完全不同的方法:
Number.isInteger = Number.isInteger || function (number) { return typeof number === 'number' && Math.floor(number) === number; };
雖然以上兩個方法均能判斷傳參是否為整數,但它們不符合ECMAScript 6規范。所以,如果你想要嚴格按照ES6的規范改寫,就請從以下語法開始:
Number.isInteger(number)
該參數number表示要測試的值。
使用此方法的示例如下所示:
// prints 'true' console.log(Number.isInteger(19)); // prints 'false' console.log(Number.isInteger(3.5)); // prints 'false' console.log(Number.isInteger([1, 2, 3]));
Node.js和所有現代瀏覽器都支持該方法,Internet Explorer除外。如果您需要支持舊版瀏覽器,則可以使用polyfill,例如火狐瀏覽器Mozilla Developer Network上提供的polyfill 。請看下面的代碼:
if (!Number.isInteger) { Number.isInteger = function isInteger (nVal) { return typeof nVal === 'number' && isFinite(nVal) && nVal > -9007199254740992 && nVal < 9007199254740992 && Math.floor(nVal) === nVal; }; }
如果您以前編寫過JavaScript代碼,則此方法對您來說并不陌生。JavaScript有一個叫做isNaN()的方法通過window對象公開。此方法用以判斷測試值是否等于NaN,是返回true,否則返回false。不過直接調用window.isNaN()有一個問題,當測試值被強制轉換為數字時,該方法會返回true值。為了讓您對此問題有一個具體的了解,以下所有語句都會返回:true
// prints 'true' console.log(window.isNaN(0/0)); // prints 'true' console.log(window.isNaN('test')); // prints 'true' console.log(window.isNaN(undefined)); // prints 'true' console.log(window.isNaN({prop: 'value'}));
您可能需要的是僅在傳遞值為NaN時返回true的方法。這就是ECMAScript 6引入Number.isNaN()
的原因。它的語法如下:
Number.isNaN(value) 這value是您要測試的值。此方法的一些示例用法如下所示: // prints 'true' console.log(Number.isNaN(0/0)); // prints 'true' console.log(Number.isNaN(NaN)); // prints 'false' console.log(Number.isNaN(undefined)); // prints 'false' console.log(Number.isNaN({prop: 'value'}));
如您所見,測試相同的值我們獲得了不同的結果。
Node和所有現代瀏覽器都支持該方法,Internet Explorer除外。如果您想支持其他瀏覽器,則此方法的一個非常簡單的polyfill如下:
Number.isNaN = Number.isNaN || function (value) { return value !== value; };
NaN是JavaScript中唯一的非自身值,這意味著它是唯一不等于自身的值。
此方法與前一個方法具有相同的背景。在JavaScript中,有這么一個方法window.isFinite()
,用于測試傳遞的值是否為有限數。不幸的是,它也會返回被強制轉換為數字的true值,示例如下所示:
// prints 'true' console.log(window.isFinite(10)); // prints 'true' console.log(window.isFinite(Number.MAX_VALUE)); // prints 'true' console.log(window.isFinite(null)); // prints 'true' console.log(window.isFinite([]));
出于這個原因,在ECMAScript 6中有一個名為isFinite()的方法。其語法如下:
Number.isFinite(value)
value是您要測試的值。如果您測試上一個代碼段中的相同值,則可以看到結果不同:
// prints 'true' console.log(Number.isFinite(10)); // prints 'true' console.log(Number.isFinite(Number.MAX_VALUE)); // prints 'false' console.log(Number.isFinite(null)); // prints 'false' console.log(Number.isFinite([]));
Node和所有現代瀏覽器都支持該方法,Internet Explorer除外。您可以在MDN上的方法頁面上找到它的polyfill。
Number.isSafeInteger()是ES6的全新補充。它測試傳遞的值是否是一個安全整數,在這種情況下它返回true。安全整數定義為滿足以下兩個條件的整數:
該數字可以精確地表示為IEEE-754雙精度數
數字的IEEE-754表示不能是舍入任何其他整數以適合IEEE-754表示的結果。
根據這個定義,安全整數是從 - (2的53次方 - 1)包含到2的53次方 - 1包含的所有整數。
Number.isSafeInteger(value) 這value是您要測試的值。此方法的一些示例用法如下所示: // prints 'true' console.log(Number.isSafeInteger(5)); // prints 'false' console.log(Number.isSafeInteger('19')); // prints 'false' console.log(Number.isSafeInteger(Math.pow(2, 53))); // prints 'true' console.log(Number.isSafeInteger(Math.pow(2, 53) - 1));
Number.isSafeInteger()在所有現代瀏覽器都支持,Internet Explorer除外。這種方法的polyfill是由Paul Miller從es6-shim中提取的,如:
Number.isSafeInteger = Number.isSafeInteger || function (value) { return Number.isInteger(value) && Math.abs(value) <= Number.MAX_SAFE_INTEGER; };
請注意,此polyfill依賴于Number.isInteger()之前討論的方法,因此您需要對后者進行polyfill以使用此方法。
ECMAScript 6還引入了兩個相關的常量值:Number.MAX_SAFE_INTEGER和Number.MIN_SAFE_INTEGER。前者表示JavaScript中的最大安全整數,即2的53次方 - 1,而后者表示最小安全整數,即 - (2的53次方 - 1)。
Number.parseInt()和Number.parseFloat()方法都屬于同一個部分,因為不像在這篇文章中提到的其他類似的方法,他們已經在以前版本的ECMAScript中存在。因此,您可以用與目前相同的方式使用它們,并獲得相同的結果。語法如下:
// Signature of Number.parseInt Number.parseInt(string, radix) // Signature of Number.parseFloat Number.parseFloat(string)
這里的string表示要解析的值,radix是您要用于轉換的基數string。
以下代碼段顯示了示例用法:
// Prints '-3' console.log(Number.parseInt('-3')); // Prints '4' console.log(Number.parseInt('100', 2)); // Prints 'NaN' console.log(Number.parseInt('test')); // Prints 'NaN' console.log(Number.parseInt({})); // Prints '42.1' console.log(Number.parseFloat('42.1')); // Prints 'NaN' console.log(Number.parseFloat('test')); // Prints 'NaN' console.log(Number.parseFloat({}));
Node和所有現代瀏覽器都支持這些方法,Internet Explorer除外。如果您想要使用它們,您可以簡單地調用它們的全局方法,如下所示:
// Polyfill Number.parseInt Number.parseInt = Number.parseInt || function () { return window.parseInt.apply(window, arguments); }; // Polyfill Number.parseFloat Number.parseFloat = Number.parseFloat || function () { return window.parseFloat.apply(window, arguments); };
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com