Javascript 尝试在其他类中实例化类时出错

Javascript 尝试在其他类中实例化类时出错,javascript,html,Javascript,Html,我试图访问一个类“Skills”,并在另一个类“Employee”中使用该类在该员工中添加技能。但是我在Employee.js:21中没有定义这个错误uncaughtreferenceerror:Skill,我不知道为什么 class Skill{ constructor(sId){ this.sId = sId; } } if (localStorage.getItem("Skill") == null) { var skillList =[];

我试图访问一个类“Skills”,并在另一个类“Employee”中使用该类在该员工中添加技能。但是我在Employee.js:21中没有定义这个错误uncaughtreferenceerror:Skill,我不知道为什么

 class Skill{
    constructor(sId){
        this.sId = sId;
    }
}
if (localStorage.getItem("Skill") == null) {
    var skillList =[];
    skillList.push(new Skill("Rekruttering"));
    skillList.push(new Skill("Bogføring"));
    skillList.push(new Skill("Engros-salg"));
    skillList.push(new Skill("JavaScript"));

  if(localStorage.getItem("Skill") == null){
      skillListString = JSON.stringify(skillList);
      localStorage.setItem("Skill", skillListString);
  }
else {
    skillList = JSON.parse(localStorage.getItem("Skill"));
  }

class Employee {
    // vi bruger en constructor funktion for at lave en opskrift på objekter af en bestemt type.
    //this metoden benyttes til at referere til det tilhørende objekt
    constructor(name, gender, department, yy, email, skills) {
        this.name = name;
        this.gender = gender;
        this.department = department;
        this.email = email;
        this.skills = [];
    }
    addNewSkill(skill){
        this.skills.push(skill);
    }
}


//Employee Database "Localstorage"
if(localStorage.getItem("Employee") == null) {
    let employeeList = [];
    employeeList.push (new Employee("Simon", "Male", "HR", 1999, "2650@mail.dk",new Skill("Sales")));
    employeeList.push (new Employee("Mads", "Male","IT", 1999,  "1234@email.com"));
    employeeList.push (new Employee("Jessica", "Female", "Sales",1998, "Mail2@mail.dk"));
    employeeList.push (new Employee("Benjamin", "Male","IT", 1997, "blabla@mail.dk"));

    if(localStorage.getItem("Employee") == null) {
        employeeListString = JSON.stringify(employeeList);
        localStorage.setItem("Employee", employeeListString);
        employeeList = JSON.parse(localStorage.getItem("Employee"));
    }
} else {
    employeeList = JSON.parse(localStorage.getItem("Employee"));
    document.querySelector("#employees").appendChild(buildTable(employeeList));
}

您需要将包含技能类的文件导入到使用它的位置

因此,您需要首先从声明的文件中导出
Skill

  • 使用文件底部的ES5:
    module.exports=Skill
  • 使用 ES6:
    导出默认技能
然后,您将在使用技能的文件顶部导入

  • 使用ES5
    const Skill=require('包含技能类的文件的位置在这里')
  • 使用ES6
    从“包含技能类的文件位置”导入技能

更多信息:

这两个类都在同一个文件中定义吗?不,这可能是原因吗?@paulmcloughlin这是怎么做到的?!我认为我的文本编辑器足够“聪明”来做这件事。我只是在用我的电脑,可惜没有。我看到了编辑。我刚得到一个语法错误。。应该忽略吗?导出默认技能;我得到了错误uncaughtsyntaxerror:Unexpected-token'export'@benjaminielsen我已经更新了我的答案,遵循ES5示例。