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?
我有一个 class Employee
,我已将其导入到一个新的 js 文件 (Main.js) 中,该文件包含一个员工列表的对象数组,但我得到一个错误 Uncaught SyntaxError: Invalid left-hand side in assignment
我附上了控制台日志中出现错误的位置。
正如您在 HTML 文件中看到的,我有两个脚本的 type = module
HTML 文件
<!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 文件
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 文件
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
因为员工是一个class而不是一个对象,我认为你必须调用构造函数。
所以这应该是员工的样子
const employees = [
new Employee(id,"Paul","Jakens","Senior Salesmen", 13000,"2015/05/15")
...
]
你也可以从构造函数中删除 id,因为你正在随机化它
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;
}
我有一个 class Employee
,我已将其导入到一个新的 js 文件 (Main.js) 中,该文件包含一个员工列表的对象数组,但我得到一个错误 Uncaught SyntaxError: Invalid left-hand side in assignment
我附上了控制台日志中出现错误的位置。
正如您在 HTML 文件中看到的,我有两个脚本的 type = module
HTML 文件
<!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 文件
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 文件
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
因为员工是一个class而不是一个对象,我认为你必须调用构造函数。
所以这应该是员工的样子
const employees = [
new Employee(id,"Paul","Jakens","Senior Salesmen", 13000,"2015/05/15")
...
]
你也可以从构造函数中删除 id,因为你正在随机化它
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;
}