Node.js v6.2.0类的扩展不是一个函数的错误? [英] Node.js v6.2.0 class extends is not a function error?
问题描述
所以我试图在节点js中扩展一个类,并且编译器继续返回以下错误:
So I'm trying to extend a class in node js and the compiler keeps returning the following error:
TypeError: Class extends value #<Object> is not a function or null
我检查了我是正确导出类,我是,任何想法?我会在下面发布我的代码:
I checked that I was exporting the class correctly and I am, any ideas? I'll post my code below:
var VenueViews = require('../views/venue'); // If I remove this the error will dissapear (as expected)
class Venue {
constructor(data) {
this.setDataHere = data;
}
main () {
var View = new VenueViews(); // This doesn't run
}
}
module.exports = Venue;
/views/venue.js:
/views/venue.js:
var Venue = require('../handlers/venue');
console.log (Venue) // This returns {} ???
class VenueViews extends Venue {
constructor() {
super();
}
}
module.exports = VenueViews;
我知道节点支持这些 es6功能
,所以我不确定为什么他们不工作?
I know that node supports these es6 features
, so I'm unsure why they aren't working?
我不确定如果这是假设发生,但是当我记录我的场馆
需要它返回一个空对象 {}
。 p>
I'm not sure if this is suppose to happen but, when I log my Venue
require it returns an empty object {}
.
console.log (Venue) // This returns {} ???
推荐答案
原来我在我的代码,其中我是将扩展的类导入到本身正在扩展的类中(扭曲者:P)。
So it turns out I had a circular reference in my code, where I was importing
the class that was extending, into the class that itself was extending (tongue twister :P).
明显的修复是简单地删除 extends
引用,找到另一种做我想要实现的方式。在我的情况下,它将 Venue
类属性传递到 VenueViews
构造函数。
The obvious fix was to simply remove the extends
reference and find another way of doing what I was trying to achieve. In my case it was passing the Venue
class properties down into the VenueViews
constructor.
例如 var x = VenueViews(this)
这篇关于Node.js v6.2.0类的扩展不是一个函数的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!