我有一個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>
類.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"
},
]
錯誤圖片
uj5u.com熱心網友回復:
因為雇員是一個類而不是一個物件,我認為你必須呼叫建構式。
所以這應該是員工的樣子
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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/485586.html
標籤:javascript 班级
