JavaScript?內(nèi)置對(duì)象?BigInt詳細(xì)解析
前言
說(shuō)起JavaScript中的內(nèi)置對(duì)象,其實(shí)又很多,今天我們介紹的是BigInt,在開發(fā)過(guò)程中,其實(shí)很少使用這個(gè)對(duì)象,所以你也不知道這個(gè)對(duì)象。它提供了一種方法來(lái)表示大于 2^53 - 1
的整數(shù)。這原本是 Javascript 中可以用 Number
表示的最大數(shù)字。BigInt
可以表示任意大的整數(shù)。
比較
它在某些方面類似于 Number
,但也有不同點(diǎn):
- 不能用于
Math
對(duì)象中的方法 - 不能和任何
Number
實(shí)例混合運(yùn)算,兩者必須轉(zhuǎn)換成同一種類型 - 在兩種類型轉(zhuǎn)換時(shí),可能會(huì)丟失精度
創(chuàng)建
const theBiggestInt = 9007199254740991n; const alsoHuge = BigInt(9007199254740991); // ? 9007199254740991n const hugeString = BigInt("9007199254740991"); // ? 9007199254740991n const hugeHex = BigInt("0x1fffffffffffff"); // ? 9007199254740991n const hugeBin = BigInt("0b11111111111111111111111111111111111111111111111111111"); // ? 9007199254740991n
方法
asIntN()
BigInt.asIntN 靜態(tài)方法將 BigInt 值轉(zhuǎn)換為一個(gè) -2^(width-1) 與 2^(width-1)-1 之間的有符號(hào)整數(shù)。
const max = 2n ** (64n - 1n) - 1n; BigInt.asIntN(64, max); // ? 9223372036854775807n BigInt.asIntN(64, max + 1n); // ? -9223372036854775808n // negative because of overflow
asUintN()
BigInt.asUintN 靜態(tài)方法將 BigInt 轉(zhuǎn)換為一個(gè) 0 和 2^width-1 之間的無(wú)符號(hào)整數(shù)。
const max = 2n ** 64n - 1n; function check64bit(number) { (number > max) ? console.log('Number doesn\'t fit in unsigned 64-bit integer!') : console.log(BigInt.asUintN(64, number)); } check64bit(2n ** 64n); // expected output: "Number doesn't fit in unsigned 64-bit integer!" check64bit(2n ** 32n); // expected output: 4294967296n
toLocaleString()
返回一個(gè)字符串,該字符串具有此 BigInt 的 language-sensitive 表達(dá)形式。
const bigint = 123456789123456789n; // German uses period for thousands console.log(bigint.toLocaleString('de-DE')); // expected output: "123.456.789.123.456.789"
它可以本地化數(shù)字格式,這樣一來(lái)也不用你專門為此功能寫API進(jìn)行轉(zhuǎn)換。為此,請(qǐng)確保使用 locales 參數(shù)指定該語(yǔ)言
var bigint = 123456789123456789n; // German uses period for thousands console.log(bigint.toLocaleString('de-DE')); // → 123.456.789.123.456.789 // Arabic in most Arabic speaking countries uses Eastern Arabic digits console.log(bigint.toLocaleString('ar-EG')); // → ??????????????????????? // India uses thousands/lakh/crore separators console.log(bigint.toLocaleString('en-IN')); // → 1,23,45,67,89,12,34,56,789 // the nu extension key requests a numbering system, e.g. Chinese decimal console.log(bigint.toLocaleString('zh-Hans-CN-u-nu-hanidec')); // → 一二三,四五六,七八九,一二三,四五六,七八九 // when requesting a language that may not be supported, such as // Balinese, include a fallback language, in this case Indonesian console.log(bigint.toLocaleString(['ban', 'id'])); // → 123.456.789.123.456.789
toString()
返回一個(gè)字符串,表示指定
BigInt
對(duì)象。 后面的 "n" 不是字符串的一部分
console.log(1024n.toString()); // expected output: "1024" console.log(1024n.toString(2)); // expected output: "10000000000" console.log(1024n.toString(16)); // expected output: "400"
valueOf()
返回 BigInt 對(duì)象包裝的原始值。
console.log(typeof Object(1n)); // expected output: "object" console.log(typeof Object(1n).valueOf()); // expected output: "bigint"
到此這篇關(guān)于JavaScript 內(nèi)置對(duì)象 BigInt詳細(xì)解析的文章就介紹到這了,更多相關(guān)JavaScript BigInt內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript 節(jié)點(diǎn)操作 以及DOMDocument屬性和方法
最近發(fā)現(xiàn)DOMDocument對(duì)象很重要,還有XMLHTTP也很重要 注意大小寫一定不能弄錯(cuò).2007-12-12JavaScript實(shí)現(xiàn)的選擇排序算法實(shí)例分析
這篇文章主要介紹了JavaScript實(shí)現(xiàn)的選擇排序算法,結(jié)合實(shí)例形式分析了選擇排序的原理、實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-04-04JavaScript架構(gòu)前端監(jiān)控搭建過(guò)程步驟
這篇文章主要為大家介紹了JavaScript架構(gòu)前端監(jiān)控搭建過(guò)程步驟,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06小程序封裝wx.request請(qǐng)求并創(chuàng)建接口管理文件的實(shí)現(xiàn)
這篇文章主要介紹了小程序封裝wx.request請(qǐng)求并創(chuàng)建接口管理文件2019-04-04JS中的oninput和onchange事件的區(qū)別及如何正確使用
在JavaScript中,oninput和onchange事件是用于處理用戶輸入的常見事件,本文將介紹oninput和onchange事件的區(qū)別,以及如何在實(shí)際開發(fā)中正確使用它們,感興趣的朋友跟隨小編一起看看吧2023-10-10JavaScript實(shí)現(xiàn)網(wǎng)頁(yè)購(gòu)物車
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)網(wǎng)頁(yè)購(gòu)物車,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06Bootstrap 實(shí)現(xiàn)表格樣式、表單布局的實(shí)例代碼
這篇文章主要介紹了Bootstrap 實(shí)現(xiàn)表格樣式、表單布局的實(shí)例代碼,需要的朋友可以參考下2018-12-12