【问题标题】:Circular dependency Super expression must either be null or a function [duplicate]循环依赖超级表达式必须为空或函数[重复]
【发布时间】:2017-08-08 11:21:46
【问题描述】:

我们有循环依赖结构,逻辑上应该没问题。 Light 导入 Node,NodeManager 又导入了 Light。当运行我们的代码时,我们得到以下

"Super expression must either be null or a function"

节点管理器

// in NodeManager.js
import Light from '../../Light.js'

class NodeManager {
  static _instance;
  static getInstance() {
    if(NodeManager._instance === undefined) {
      NodeManager._instance = new NodeManager();
    }
   return NodeManager._instance;
  }
  ...
  addNode(node){
    ...
    if(node instanceOf Light){
      ...
    }
    ...
  }
}

节点

// in Node.js
import NodeManager from '../../NodeManager.js'
class Node {
  constructor() {
    ...
    NodeManager.instance().addNode(this);
    ...
  }
}

// in Light.js
import Node from '../../Node.js'
class Light extends Node {
  constructor() {
    super();        
    ...
  }
}

由于某种原因,Light 中的 Node 导入不起作用,因此 Light.js 在调用 super 时会抛出错误。我不确定如何解决这个问题。任何帮助表示赞赏。

【问题讨论】:

  • 你解决了吗?为同样的事情苦苦挣扎。

标签: javascript import ecmascript-6 circular-dependency


【解决方案1】:

super() 必须是您在构造函数中调用的第一件事(请参阅here)。 您似乎在 Node 类的构造函数中缺少 super() 调用。

【讨论】:

  • 不,我称之为超级,我只是为了简洁而忽略了它。
猜你喜欢
  • 2023-04-10
  • 1970-01-01
  • 2019-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-05
  • 1970-01-01
相关资源
最近更新 更多