Node.js v6.2.0类扩展不是函数错误
Node.js v6.2.0 class extends is not a function error?
所以我试图在node js中扩展一个类,编译器不断返回以下错误:
TypeError: Class extends value #<Object> is not a function or null
我检查了我是否正确导出了类,我是,有什么想法吗?我将在下面发布我的代码:
/handler/venue.js:
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:
var Venue = require('../handlers/venue');
console.log (Venue) // This returns {} ???
class VenueViews extends Venue {
constructor() {
super();
}
}
module.exports = VenueViews;
我知道节点支持这些es6 features
,所以我不确定它们为什么不工作?
编辑:
我不确定是否会发生这种情况,但当我记录我的Venue
需求时,它会返回一个空对象{}
。
console.log (Venue) // This returns {} ???
所以我的代码中有一个循环引用,我是importing
,即正在扩展的类,到它本身正在扩展的类中(绕口令:p)。
显而易见的解决方案是简单地删除extends
引用,并找到另一种方法来实现我想要实现的目标。在我的例子中,它将Venue
类属性向下传递到VenueViews
构造函数中。
例如var x = VenueViews(this)
在我的例子中,由于我试图在Typescript中设置工厂模式,这与@James11遇到的问题(循环导入)相同。我的解决方案是将代码移动到文件中,类似于以下内容:
// ./src/interface.ts
import { ConcreteClass } from './concrete';
export interface BaseInterface {
someFunction(): any;
}
export class Factory {
static build(): BaseInterface {
return new ConcreteClass();
}
}
// ./src/base.ts
import { BaseInterface } from './interface';
class BaseClass implements BaseInterface {
someFunction(): any {
return true;
}
}
// ./src/concrete.ts
import { BaseClass } from './base';
export class ConcreteClass extends BaseClass {
someFunction(): any {
return false;
}
}
我也遇到过类似的问题,在检查了所有解决方法后,通过删除node_modules文件夹并运行npm i
最终解决了问题。
相关文章:
- Node.js v6.2.0类扩展不是函数错误
- Javascript搜索函数错误
- Chrome Javascript日期构造函数错误
- 浏览器中出现javascript初始大写函数错误
- Createjs冲突问题localToLocal不是函数错误
- Java脚本函数错误
- 更新带有Javascript函数错误的变量
- 如何将对象插入对象列表的开头,当它有 unshift 不是函数错误
- 在将 Pickadate 与 Meteor 一起使用时,未定义不是函数错误
- 运行咕噜咕噜咕噜抛出 _.object 不是函数错误
- 了解函数(错误、数据)回调
- javascript中的typeof()函数错误
- 我一直在获取angularjs服务不是函数错误(LoginService.login不是函数)
- HTML5拖拽&drop正在抛出undefined不是函数错误
- Google Maps API a.lat不是函数错误
- getItemAtIndex不是函数错误
- Javascript停止函数错误
- 对话框函数错误无效函数
- Javascript 自定义函数错误
- 因果报应单元测试'未定义'不是函数错误