JavaScript实现人事管理系统
下面是一个基于JavaScript的简单人事管理系统实现方案,包含核心功能和代码示例:
核心功能模块
class Employee {
constructor(id, name, department, position, salary) {
this.id = id; // 员工ID
this.name = name; // 姓名
this.department = department; // 部门
this.position = position; // 职位
this.salary = salary; // 薪资
}
}
class HRSystem {
constructor() {
this.employees = []; // 员工数据库
this.nextId = 1; // 自增ID计数器
}
// 添加新员工
addEmployee(name, department, position, salary) {
const newEmployee = new Employee(
this.nextId++,
name,
department,
position,
salary
);
this.employees.push(newEmployee);
return newEmployee;
}
// 删除员工
removeEmployee(id) {
const index = this.employees.findIndex(emp => emp.id === id);
if (index !== -1) {
return this.employees.splice(index, 1)[0];
}
return null;
}
// 更新员工信息
updateEmployee(id, updateData) {
const employee = this.employees.find(emp => emp.id === id);
if (employee) {
Object.assign(employee, updateData);
return employee;
}
return null;
}
// 查询员工
findEmployee(criteria) {
return this.employees.filter(emp =>
Object.keys(criteria).every(key =>
emp[key] === criteria[key]
)
);
}
// 生成部门报告
generateDepartmentReport() {
const report = {};
this.employees.forEach(emp => {
if (!report[emp.department]) {
report[emp.department] = { count: 0, totalSalary: 0 };
}
report[emp.department].count++;
report[emp.department].totalSalary += emp.salary;
});
return report;
}
}
使用示例
// 初始化系统
const myHR = new HRSystem();
// 添加员工
myHR.addEmployee("张三", "技术部", "前端工程师", 15000);
myHR.addEmployee("李四", "技术部", "后端工程师", 18000);
myHR.addEmployee("王五", "市场部", "营销经理", 20000);
// 更新员工信息
myHR.updateEmployee(2, { position: "高级工程师", salary: 22000 });
// 查询技术部员工
const techDept = myHR.findEmployee({ department: "技术部" });
console.log("技术部员工:", techDept);
// 生成部门报告
const report = myHR.generateDepartmentReport();
console.log("部门统计报告:", report);
// 删除员工
myHR.removeEmployee(3);
console.log("删除后剩余员工:", myHR.employees);
功能扩展建议
-
数据持久化:添加
localStorage支持saveToLocalStorage() { localStorage.setItem('hrData', JSON.stringify({ employees: this.employees, nextId: this.nextId })); } loadFromLocalStorage() { const data = JSON.parse(localStorage.getItem('hrData')); if (data) { this.employees = data.employees; this.nextId = data.nextId; } } -
薪资计算功能:
calculatePayroll() { return this.employees.reduce((total, emp) => total + emp.salary, 0); } -
工龄跟踪:
class Employee { constructor(/*...*/ joinDate = new Date()) { // ... this.joinDate = joinDate; } getYearsOfService() { const diff = new Date() - this.joinDate; return Math.floor(diff / (1000 * 60 * 60 * 24 * 365)); } }
系统特点
- 使用面向对象设计,符合人事管理业务逻辑
- 支持完整的CRUD操作(创建、读取、更新、删除)
- 提供部门统计分析功能
- 采用纯JavaScript实现,无第三方依赖
- 代码结构清晰,易于扩展和维护
此实现可作为基础版本,实际部署时可结合前端框架(如React/Vue)构建用户界面,或使用Node.js+Express构建后端API服务。
957

被折叠的 条评论
为什么被折叠?



