Javascript Node.js v6.2.0 类扩展不是函数错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37312197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:11:43  来源:igfitidea点击:

Node.js v6.2.0 class extends is not a function error?

javascriptnode.jsecmascript-6

提问by James111

So I'm trying to extend a class in node js and the compiler keeps returning the following error:

所以我试图在 node js 中扩展一个类,编译器不断返回以下错误:

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:

我检查了我是否正确导出了课程,我是,有什么想法吗?我将在下面发布我的代码:

/handler/venue.js:

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

/views/venue.js:

var Venue = require('../handlers/venue');
console.log  (Venue) // This returns {} ???

class VenueViews extends Venue {
  constructor() {
    super();
  }
}

module.exports = VenueViews;

I know that node supports these es6 features, so I'm unsure why they aren't working?

我知道 node 支持这些es6 features,所以我不确定为什么它们不起作用?

Edit:

编辑:

I'm not sure if this is suppose to happen but, when I log my Venuerequire it returns an empty object {}.

我不确定这是否会发生,但是,当我记录我的Venuerequire 时,它​​返回一个空对象{}

console.log  (Venue) // This returns {} ???

回答by James111

So it turns out I had a circular reference in my code, where I was importingthe class that was extending, into the class that itself was extending (tongue twister :P).

所以事实证明我在我的代码中有一个循环引用,我是importing正在扩展的类,到它本身正在扩展的类中(绕口令:P)。

The obvious fix was to simply remove the extendsreference and find another way of doing what I was trying to achieve. In my case it was passing the Venueclass properties down into the VenueViewsconstructor.

显而易见的解决方法是简单地删除extends引用并找到另一种方式来做我想要实现的目标。在我的情况下,它将Venue类属性传递给VenueViews构造函数。

E.g var x = VenueViews(this)

例如 var x = VenueViews(this)

回答by Blaskovicz

In my instance, it was the same issue as @James111 was experiencing (circular import) due to a factory pattern I was trying to set up in Typescript. My fix was to do move the code into files, similar to the following:

在我的例子中,由于我试图在 Typescript 中设置工厂模式,这与 @James111 遇到的问题(循环导入)相同。我的解决方法是将代码移动到文件中,类似于以下内容:

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