Home > Software engineering >  Uncaught SyntaxError: Invalid left-hand side in assignment: I am not sure what is causing this error
Uncaught SyntaxError: Invalid left-hand side in assignment: I am not sure what is causing this error

Time:06-04

I have a class Employee that I have imported into a new js file (Main.js) that has an array of objects of a list of employees, but I get an error of Uncaught SyntaxError: Invalid left-hand side in assignment I have attached where I get the error in the console log.

As you can see in the HTML file I have the type = module for both scripts

HTML file

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="module" src="classes.js" defer></script>
    <script type="module" src="main.js" defer></script>
    <title>Javascript Practice</title>
</head>

Classes.js file

export class Employee{
    constructor(
        id,
        name,
        surname,
        jobtitle,
        salary,
        startDate
        ){
       this.id = id;
       this.name = name;
       this.surname = surname;
       this.jobtitle = jobtitle;
       this.salary = salary;
       this.startDate = startDate;
    }
    id = Math.floor(Math.random() * 1000)   (new Date()).getTime();
}

Main.js file

import { Employee } from "../classes.js"

const employees =[
    new Employee = {
        id:id,
        name:"Paul",
        surname:"Jakens",
        jobtitle:"Senior Salesmen",
        salary: 13000,
        startDate:"2015/05/15"
    },
    new Employee = {
        id:id,
        name:"Susan",
        surname:"Lupan",
        jobtitle:"Junior Designer",
        salary: 12000,
        startDate:"2021/03/16"
    },
    new Employee = {
        id:id,
        name:"John",
        surname:"Angel",
        jobtitle:"Senior Full Stack Developer",
        salary: 40000,
        startDate:"2014/10/18"
    },
]

Image of error

CodePudding user response:

Because the employee is a class instead of an object, I think you have to call the constructor.

So this should be what the employees would look like

const employees = [
  new Employee(id,"Paul","Jakens","Senior Salesmen", 13000,"2015/05/15")
  ...
]

Also you can remove the id from the constructor since you're randomizing it

constructor(
        name,
        surname,
        jobtitle,
        salary,
        startDate
        ){
       this.id = Math.floor(Math.random() * 1000)   (new Date()).getTime();;
       this.name = name;
       this.surname = surname;
       this.jobtitle = jobtitle;
       this.salary = salary;
       this.startDate = startDate;
    }
  • Related