Reputation: 139
Im trying to access a class "Skills" and using the class within a other class "Employee" to add skills within the employee. but I get this error Uncaught ReferenceError: Skill is not defined at Employee.js:21
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"));
}
constructor(name, gender, department, yy, email, skills) {
this.name = name;
this.gender = gender;
this.department = department;
this.email = email;
this.skills = 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, "[email protected]", new Skill("Sales")));
employeeList.push (new Employee("Mads", "Male","IT", 1999, "[email protected]"));
employeeList.push (new Employee("Jessica", "Female", "Sales",1998, "[email protected]"));
employeeList.push (new Employee("Benjamin", "Male","IT", 1997, "[email protected]"));
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));
}
Upvotes: 0
Views: 43
Reputation: 3033
You need to initialize the skills array. So you need to always specify the 6th param and always have it be an array like @junvar says or use a default value (it still needs to be an array when you do specify it, ie [new Skill("Sales")]
)
class Skill {
/**
* @param {string} sId
*/
constructor(sId){
this.sId = sId;
}
}
class Employee {
/**
* @param {string} name
* @param {string} gender
* @param {string} department
* @param {number} yy
* @param {string} email
* @param {Skill[]} [skills=[]] - initial skills, defaults to empty array
*/
constructor(name, gender, department, yy, email, skills = []) {
this.name = name;
this.gender = gender;
this.department = department;
this.email = email;
this.skills = skills;
}
/**
* @param {Skill} skill - new skill to add
*/
addNewSkill(skill) {
this.skills.push(skill);
}
}
const e1 = new Employee("Simon", "Male", "HR", 1999, "[email protected]", [new Skill("Sales")]);
e1.addNewSkill(new Skill("JS"));
console.log(e1.skills.map(s => s.sId)); // [ "Sales", "JS" ]
const e2 = new Employee("Mads", "Male","IT", 1999, "[email protected]"); // only 5 params works because of `skills = []` above
e2.addNewSkill(new Skill("JS"));
console.log(e2.skills.map(s => s.sId)); // [ "JS" ]
Upvotes: 0
Reputation: 11574
You're assigning a non-array object to your skills
field, and then calling skills.push
as if it were an array. Simply assign an array instead.
new Employee(..., [new Skill("Sales")]);
Upvotes: 1