代码之家  ›  专栏  ›  技术社区  ›  khateeb

JS的decalring类和对象出错

  •  0
  • khateeb  · 技术社区  · 7 年前

    我的班级声明是:

    class Category {
      constructor(id, name, type, url) {
        this.id = id;
        this.name = name;
        this.type = type;
        this.url = url;
        this.assets = [];
      }
    
      set assets(value) {
        this.assets = value;
      }
    }
    

    我把它导入了我的主类 import Category from './beans'; 我试图用

    let category = new Category("1", "2", "3", "4");
    

    我得到错误:

    undefined is not a constructor (evaluating 'new _beans.Category("1", "2", "3", "4")')
    

    如何解决这个问题?


    编辑 似乎 assets 在的构造函数中声明 Category 导致错误。当我把它取下来时,它起作用了。现在的问题是如何在 类别 类,该类稍后将初始化,但不在构造函数中?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Michael Powers    7 年前

    class Category {
        constructor(id, name, type, url) {
            this.id = id;
            this.name = name;
            this.type = type;
            this.url = url;
            this.assets = [];
            this.myVar = null;
        }
        setMyVar(myVar) {
            this.myVar = myVar;
        }
    }