浅谈BigInt和symbol

424 阅读3分钟

浅谈BigInt和symbol

  1. BigInt

    概念:JavaScript 中的任意精度的整数

    1. 使用

    • 示例

      // 1.整数字面量后面加 n
      console.log(typeof 9007199254740991n) // bigint
      console.log(typeof '9007199254740991n') // 注意 'number+n',是 string类型
      
      // 2.BigInt
      const hugeNum = BigInt(9007199254740991);
      const hugeString = BigInt("9007199254740991");
      const huge = BigInt("0x12");
      // const errNum = BigInt("9007199254740991a");//抛出错误
      
      console.log('hugeNum',hugeNum,typeof hugeNum)
      console.log('hugeString',hugeString,typeof hugeString)
      console.log('huge',huge,typeof huge)
      

    2.解决number类型精度丢失问题

    1. Number精度有限

      只能安全地表示-(2^53-1) 到(2^53-1)之间的整数,大于JS Number 类型所能表示的最大整数,将自动四舍五入 ;

      Number.MIN_SAFE_INTEGER:-(2^53-1)

      Number.MAX_SAFE_INTEGER:2^53-1 Number.MAX_VALUE:表示最大浮点数,大约是 1.798e+308 Number.MIN_VALUE:表示最小浮点数,大约是 5e-324

      const max = Math.pow(2,53) - 1; //9007199254740991 Number类型所能表示的最大整数
      
      console.log(Number(9007199254740992) === Number(9007199254740993)); //true
      console.log(9007199254740993); // 9007199254740992
      console.log(9999999999999999); // 10000000000000000
      
    2. 解决number数据精度丢失问题

      console.log('BigInt----',9007199254740992n,BigInt("9007199254740992"));
      console.log('BigInt----',9007199254740993n,BigInt("9007199254740993"));
      console.log('BigInt----',9999999999999999n,BigInt("9999999999999999"));
      

    3.运算

    • 示例

      只能和BigInt数据进行运算

      const maxPlusOne = 9007199254740991n + 1n;
      // ↪ 9007199254740992n
      
      const subtr = 9007199254740991n10n;
      // ↪ 9007199254740981n
      
      const multi = 9007199254740991n * 2n;
      // ↪ 18014398509481982n
      
      const mod = 9007199254740991n / 10n;
      // ↪ 4503599627370495n 带小数的运算会被向下取整
      
      const mod = 9007199254740991n % 10n;
      // ↪ 1n
      

    4.比较

    1. 与BigInt类型数据比较

      // ↪ true
      
      2n > 1n
      // ↪ true
      
      2n >= 2n
      // ↪ true
      
    2. 与Number类型数据比较

      1n < 2n
      // ↪ true
      
      2n > 1
      // ↪ true
      
      2n >= 2
      // ↪ true
      
    3. BigInt和Number不是严格相等的,但是宽松式相等

      0n === 0
      // ↪ false
      
      0n == 0
      //↪ true
      
  2. symbol

    • 新的原始数据类型Symbol,表示独一无二的值。

    • 直接调用Symbol函数即可生成一个Symbol

    • Symbol函数可以接受一个字符串作为参数,表示对 Symbol 的描述,主要是为了在控制台显示,或者转为字符串时,比较容易区分。

      let s1 = Symbol('foo');
      let s2 = Symbol('bar');
      
      s1 // Symbol(foo)
      s2 // Symbol(bar)



      
    • 注意,Symbol函数的参数只是表示对当前 Symbol 值的描述,因此相同参数的Symbol函数的返回值是不相等的。

      // 没有参数的情况
      let s1 = Symbol();
      let s2 = Symbol();
      
      s1 === s2 // false
      
      // 有参数的情况
      let s1 = Symbol('foo');
      let s2 = Symbol('foo');
      
      s1 === s2 // false



      
    • Symbol.for

      它接受一个字符串作为参数,然后搜索有没有以该参数作为名称的 Symbol 值。如果有,就返回这个 Symbol 值,否则就新建并返回一个以该字符串为名称的 Symbol 值

      let s1 = Symbol.for('foo');
      let s2 = Symbol.for('foo');
      
      s1 === s2 // true