Javascript &引用;“不是构造函数”;,但这些类型的人都会离开

Javascript &引用;“不是构造函数”;,但这些类型的人都会离开,javascript,node.js,constructor,Javascript,Node.js,Constructor,是的,以前有人问过这个问题,但对我来说是有用的 调用Config构造函数时,我得到一个TypeError:Config不是构造函数。通过查看其他SO问题,发现此错误的常见原因可能是隐藏构造函数或调用不可调用的类型,但这两种情况都不会在我的项目中检出 这是电话: var Server = require("./server.js").Server; var Config = require("./config.js").Config; new Server(new Config("app/con

是的,以前有人问过这个问题,但对我来说是有用的

调用
Config
构造函数时,我得到一个
TypeError:Config不是构造函数。通过查看其他SO问题,发现此错误的常见原因可能是隐藏构造函数或调用不可调用的类型,但这两种情况都不会在我的项目中检出

这是电话:

var Server = require("./server.js").Server;
var Config = require("./config.js").Config;

new Server(new Config("app/config.json")).run();
/config.js
中:

var fs = require("fs");

exports.Config = file => {
  var json;

  if (fs.existsSync(file) && !fs.lstatSync(file).isDirectory()) {
    json = JSON.parse(fs.readFileSync(file));
  }
  else {
    throw new ReferenceError("File doesn't exist: can't load config");
  }

  this.has = key => {
    return json.hasOwnProperty(key);
  };

  this.get = key => {
    return json[key] || null;
  };

  this.set = (key, value, write) => {
    json[key] = value;
    if (write) {
      fs.writeFileSync(file, JSON.stringify(json));
    }
  };
};
在调用之前记录
Config
的类型会发现它是一个
函数
,因此几乎可以肯定它与
Config.js
中定义的函数相同。那么,为什么Node告诉我它不是构造函数

那么,为什么Node告诉我它不是构造函数

因为它不是构造函数。:-)箭头函数从来都不是构造函数,它们关闭在
this
上,没有
prototype
属性,因此不能用作构造函数(当通过
new
调用它们时,需要设置一个特定的
this
,并且需要有
prototype
属性,以便可以使用它来设置[[prototype]]通过
new
)创建的对象的

要么1。将其设为
函数
函数或2。将其设置为

以下是对#1的一行更改:

exports.Config = function(file) {