Javascript ES6 TypeError:没有'new'就不能调用类构造函数客户端
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/51860043/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript ES6 TypeError: Class constructor Client cannot be invoked without 'new'
提问by Akshay Sood
I have a class written in Javascript ES6. When I try to execute nodemoncommand I always see this error TypeError: Class constructor Client cannot be invoked without 'new'
我有一个用 Javascript ES6 编写的类。当我尝试执行nodemon命令时,我总是看到此错误TypeError: Class constructor Client cannot be invoked without 'new'
The full error is mentioned below:
下面提到了完整的错误:
/Users/akshaysood/Blockchain/fabricSDK/dist/application/Transaction.js:45
        return (0, _possibleConstructorReturn3.default)(this, (FBClient.__proto__ || (0, _getPrototypeOf2.default)(FBClient)).call(this, props));
                                                                                                                              ^
TypeError: Class constructor Client cannot be invoked without 'new'
    at new FBClient (/Users/akshaysood/Blockchain/fabricSDK/dist/application/Transaction.js:45:127)
    at Object.<anonymous> (/Users/akshaysood/Blockchain/fabricSDK/dist/application/Transaction.js:195:14)
    at Module._compile (module.js:641:30)
    at Object.Module._extensions..js (module.js:652:10)
    at Module.load (module.js:560:32)
    at tryModuleLoad (module.js:503:12)
    at Function.Module._load (module.js:495:3)
    at Module.require (module.js:585:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/akshaysood/Blockchain/fabricSDK/dist/routes/users.js:11:20)
What I am trying to do is, I have created a class and then created an instance of that class. Then I am trying to export that variable.
我想要做的是,我创建了一个类,然后创建了该类的一个实例。然后我试图导出该变量。
The class structure is defined below:
类结构定义如下:
class FBClient extends FabricClient{
    constructor(props){
        super(props);
    }
<<< FUNCTIONS >>>
}
How I am trying to export the variable ->
我如何尝试导出变量 ->
var client = new FBClient();
client.loadFromConfig(config);
export default client = client;
You can find the full code here > https://hastebin.com/kecacenita.jsCode generated by Babel > https://hastebin.com/fabewecumo.js
你可以在这里找到完整的代码 > https://hastebin.com/kecacenita.jsBabel 生成的代码 > https://hastebin.com/fabewecumo.js
回答by Estus Flask
The problem is that the class extends native ES6 class and is transpiled to ES5 with Babel. Transpiled classes cannot extend native classes, at least without additional measures.
问题是该类扩展了原生 ES6 类,并使用 Babel 转换为 ES5。转译的类不能扩展本地类,至少在没有额外措施的情况下是这样。
class TranspiledFoo extends NativeBar {
  constructor() {
    super();
  }
}
results in something like
结果像
function TranspiledFoo() {
  var _this = NativeBar.call(this) || this;
  return _this;
}
// prototypically inherit from NativeBar 
Since ES6 classes should be only called with new, NativeBar.callresults in error.
由于 ES6 类只能使用 调用new,因此会NativeBar.call导致错误。
ES6 classes are supported in any recent Node version, they shouldn't be transpiled. es2015should be excluded from Babel configuration, it's preferable to use envpreset set to nodetarget.
任何最新的 Node 版本都支持 ES6 类,它们不应该被转译。es2015应该从 Babel 配置中排除,最好使用env预设设置为nodetarget。
The same problem applies to TypeScript. The compiler should be properly configured to not transpile classes in order for them to inherit from native or Babel classes.
同样的问题也适用于 TypeScript。编译器应该正确配置为不转译类,以便它们从本机或 Babel 类继承。
回答by Chandan Aswal
In package.jsonyou can use targets configuration with @babel/preset-env.
set the esmodulesas 'true'.
在package.json你可以使用目标配置与@babel/preset-env。设置esmodules为“真”。
Below is the example how I am using in my file:
下面是我如何在我的文件中使用的示例:
  "babel": {
    "presets": [
      [
        "@babel/preset-env",
        {
          "targets": {
            "esmodules": true
          }
        }
      ],
      "@babel/preset-react",
      "@babel/preset-flow"
    ],
    "plugins": [
      "@babel/plugin-proposal-class-properties"
    ]
  },
回答by KajMagnus
I was transpiling not Javascript but Typescript, and ran into the same problem.
我转译的不是 Javascript 而是 Typescript,遇到了同样的问题。
I edited the Typescript compiler config file, tsconfig.json, to generate ES2017 Javascript code:
我编辑了 Typescript 编译器配置文件tsconfig.json,以生成 ES2017 Javascript 代码:
{
    "compilerOptions": {
        "target": "ES2017",
instead of whatever the default is, ES2015? — then all worked fine.
而不是默认值,ES2015?- 然后一切正常。
(Maybe this answer can be helpful for people who use Typescript and find this question when they search for the same error message, like I did.)
(也许这个答案对使用 Typescript 并在搜索相同错误消息时找到这个问题的人有所帮助,就像我一样。)
回答by Devo
For those who are using ts-node, it could be possible that your tsconfig.jsonis unable to be loaded by ts-node.
First make sure you've set the below option for tsconfig.json:
对于那些正在使用ts-node 的人,您可能tsconfig.json无法被 ts-node 加载。
首先确保您已为以下选项设置tsconfig.json:
{
    "compilerOptions": {
        "target": "ES6",
        ...
    }
}
Then you may try ts-node --script-modeor use --projectto specify the path to your tsconfig.json.
然后您可以尝试ts-node --script-mode或使用--project来指定您的tsconfig.json.
回答by Alejandro
I hope you have already been able to solve your problem, here is my solution for this error:
我希望您已经能够解决您的问题,这是我针对此错误的解决方案:
Blockquote
块引用
class indexRoutes
{
    public  router: Router= Router();
}
const INDEX_ROUTES = new indexRoutes();
export default INDEX_ROUTES.router;

