本文介绍了Javascript ES6 TypeError:如果没有"new",则无法调用类构造函数Client的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用Javascript ES6编写的类.当我尝试执行nodemon命令时,我总是会看到此错误TypeError: Class constructor Client cannot be invoked without 'new'

I have a class written in Javascript ES6. When I try to execute nodemon command I always see this error TypeError: Class constructor Client cannot be invoked without 'new'

完整错误如下所述:

/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.

类结构定义如下:

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;

您可以在此处找到完整的代码> https://hastebin.com/kecacenita.js Babel> https://hastebin.com/fabewecumo.js

You can find the full code here > https://hastebin.com/kecacenita.jsCode generated by Babel > https://hastebin.com/fabewecumo.js

推荐答案

问题是该类扩展了本机ES6类,并通过Babel转换为ES5.转译的类至少在没有其他措施的情况下不能扩展本机类.

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.

class TranspiledFoo extends NativeBar {
  constructor() {
    super();
  }
}

结果类似

function TranspiledFoo() {
  var _this = NativeBar.call(this);
  return _this;
}
// prototypically inherit from NativeBar

由于仅应使用new调用ES6类,因此NativeBar.call会导致错误.

Since ES6 classes should be only called with new, NativeBar.call results in error.

ES6类在任何最新的Node版本中均受支持,不应进行编译. es2015应该从Babel配置中排除,最好使用 env预设设置为node目标.

ES6 classes are supported in any recent Node version, they shouldn't be transpiled. es2015 should be excluded from Babel configuration, it's preferable to use env preset set to node target.

相同的问题适用于TypeScript .编译器应正确配置为不转译类,以便它们可以从本机或Babel类继承.

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.

这篇关于Javascript ES6 TypeError:如果没有"new",则无法调用类构造函数Client的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 01:46