Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Babel中使用BigInt求幂?_Javascript_Babeljs - Fatal编程技术网

Javascript 如何在Babel中使用BigInt求幂?

Javascript 如何在Babel中使用BigInt求幂?,javascript,babeljs,Javascript,Babeljs,考虑这一点: const a = BigInt(2); const b = BigInt(2); const c = a ** b; 巴贝尔将此转换为: var a = BigInt(2); var b = BigInt(2); var c = Math.pow(a, b); 但是,Math.pow不适用于BigInt。据我所知,要让巴贝尔忽略某一行是不可能的。我找到了babel插件transform bigint,但我不想为此加载polyfill。如果不支持BigInt,那么我将为输入设置

考虑这一点:

const a = BigInt(2);
const b = BigInt(2);
const c = a ** b;
巴贝尔将此转换为:

var a = BigInt(2);
var b = BigInt(2);
var c = Math.pow(a, b);
但是,
Math.pow
不适用于
BigInt
。据我所知,要让巴贝尔忽略某一行是不可能的。我找到了
babel插件transform bigint
,但我不想为此加载polyfill。如果不支持
BigInt
,那么我将为输入设置一个上限

我的选项是覆盖
Math.pow
或手动实现求幂。目前是否无法将本机
**
运算符与
BigInt
和Babel一起使用

另外,如果不支持,
**
将是一个语法错误,对吗

编辑:babel.config.js:

module.exports = {
  presets: [
    ['@babel/preset-env', {
      useBuiltIns: 'usage',
      corejs: '2',
      exclude: [
        'babel-plugin-transform-async-to-generator',
        'babel-plugin-transform-regenerator',
      ],
    }],
  ],
  plugins: [
    '@babel/plugin-syntax-dynamic-import',
    ['@babel/plugin-transform-react-jsx', { pragma: 'h' }],
    '@babel/plugin-proposal-do-expressions',
    '@babel/plugin-proposal-class-properties',
    '@babel/plugin-proposal-optional-chaining',
    /*
    Async/await increases file size by a lot.
    ['module:fast-async', {
      'compiler': { 'promises': true, 'generators': false, 'useRuntimeModule': true },
    }],
    ['@babel/plugin-transform-modules-commonjs', {
      'strictMode': false,
    }],
    */
  ],
  env: {
    production: {
      plugins: ['transform-react-remove-prop-types'],
    },
  },
  sourceType: 'unambiguous',
};

是否愿意与大家分享你的
.babelrc
?为了能够在本机bigint上使用
**
,我想你要么需要
新函数
或者
eval
来处理动态语法(否则会抛出语法错误)。也许可以考虑使用一个类似于BigTnT的库?<代码> EVA/COD>很有趣,这可能是唯一的方法。georg添加了Babel配置。谢谢。如果希望**保持原样,请将
babel插件变换求幂运算符添加到排除部分。