JavaScript?內(nèi)置對(duì)象?BigInt詳細(xì)解析
前言
說起JavaScript中的內(nèi)置對(duì)象,其實(shí)又很多,今天我們介紹的是BigInt,在開發(fā)過程中,其實(shí)很少使用這個(gè)對(duì)象,所以你也不知道這個(gè)對(duì)象。它提供了一種方法來表示大于 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 之間的無符號(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: 4294967296ntoLocaleString()
返回一個(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ù)字格式,這樣一來也不用你專門為此功能寫API進(jìn)行轉(zhuǎn)換。為此,請(qǐng)確保使用 locales 參數(shù)指定該語言
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.789toString()
返回一個(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中校驗(yàn)銀行卡號(hào)的實(shí)現(xiàn)代碼
本文通過案例給大家介紹了js中校驗(yàn)銀行卡號(hào)的代碼,代碼小編測(cè)試過,可行。代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2016-12-12
微信小程序動(dòng)態(tài)評(píng)分展示/五角星展示/半顆星展示/自定義長(zhǎng)度展示功能的實(shí)現(xiàn)
這篇文章主要介紹了微信小程序動(dòng)態(tài)評(píng)分展示/五角星展示/半顆星展示/自定義長(zhǎng)度展示的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
用JS操作FRAME中的IFRAME及其內(nèi)容的實(shí)現(xiàn)代碼
一直都需要這樣的東西,發(fā)現(xiàn)了這個(gè)好東西,一定要研究下2008-07-07
JavaScript讓網(wǎng)頁出現(xiàn)漸隱漸顯背景顏色的方法
這篇文章主要介紹了JavaScript讓網(wǎng)頁出現(xiàn)漸隱漸顯背景顏色的方法,涉及javascript操作樣式的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04

