亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JavaScript?內(nèi)置對(duì)象?BigInt詳細(xì)解析

 更新時(shí)間:2022年07月15日 10:02:37   作者:??桃子蘸醬?  
這篇文章主要介紹了JavaScript?內(nèi)置對(duì)象?BigInt詳細(xì)解析,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下

前言

說(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)文章

最新評(píng)論