Javascript ES6 TypeError:没有'new'就不能调用类构造函数客户端 [英] Javascript ES6 TypeError: Class constructor Client cannot be invoked without 'new'

查看:33
本文介绍了Javascript ES6 TypeError:没有'new'就不能调用类构造函数客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 Javascript ES6 编写的类.当我尝试执行 nodemon 命令时,我总是看到这个错误 TypeError: Class constructor Client cannot be called 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.jsBabel 生成的代码 > https://hastebin.com/fabewecumo.js

You can find the full code here > https://hastebin.com/kecacenita.js Code 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) || this;
  return _this;
}
// prototypically inherit from NativeBar 

由于 ES6 类只能使用 new 调用,NativeBar.call 会导致错误.

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

任何最新的 Node 版本都支持 ES6 类,它们不应被转译.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'就不能调用类构造函数客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆