如何用ES5实现支持多类型员工的EmployeeCollection集合类
实现说明
先修复原有代码的已知错误,再完成EmployeeCollection集合类的ES5实现:
第一步:修复原有代码问题
原有AbstractEmployee.js存在未定义变量object的逻辑错误,且子类缺少原型链继承绑定,修正后代码如下:
// AbstractEmployee.js var AbstractEmployee = function(id, name, salary) { if (this.constructor === AbstractEmployee) { throw new Error("不能实例化抽象类!"); } // 修正原代码未定义变量的参数校验逻辑 if(typeof id !== 'string' || typeof name !== 'string' || typeof salary !== 'number'){ throw new Error("传入参数错误!"); } this.id = id; this.name = name; this.salary = salary; }; AbstractEmployee.prototype.getSalary = function() { throw new Error('getSalary()方法必须被重写实现'); }; module.exports = AbstractEmployee;
// PerHourSalaryEmployee.js var AbstractEmployee = require('./AbstractEmployee.js'); // ES5原型链继承绑定 PerHourSalaryEmployee.prototype = Object.create(AbstractEmployee.prototype); PerHourSalaryEmployee.prototype.constructor = PerHourSalaryEmployee; function PerHourSalaryEmployee(index, name, hourRate) { // 先拼接符合要求的id字符串再传入父类 var id = 'id' + index; // 计算按月计的小时工薪资 var monthlySalary = hourRate * 20.88 * 8; AbstractEmployee.call(this, id, name, monthlySalary); } PerHourSalaryEmployee.prototype.getSalary = function() { return this.salary; }; module.exports = PerHourSalaryEmployee;
// FixedSalaryEmployee.js var AbstractEmployee = require('./AbstractEmployee.js'); // ES5原型链继承绑定 FixedSalaryEmployee.prototype = Object.create(AbstractEmployee.prototype); FixedSalaryEmployee.prototype.constructor = FixedSalaryEmployee; function FixedSalaryEmployee(index, name, fixedSalary) { var id = 'id' + index; AbstractEmployee.call(this, id, name, fixedSalary); } FixedSalaryEmployee.prototype.getSalary = function() { return this.salary; }; module.exports = FixedSalaryEmployee;
第二步:实现EmployeeCollection集合类
// EmployeeCollection.js var PerHourSalaryEmployee = require('./PerHourSalaryEmployee.js'); var FixedSalaryEmployee = require('./FixedSalaryEmployee.js'); function EmployeeCollection(employeeJsonData) { this.employees = []; var self = this; // 遍历JSON数据实例化对应类型的员工对象 employeeJsonData.forEach(function(item, index) { var employee; if (item.type === 'per-hour') { employee = new PerHourSalaryEmployee(index, item.name, item.salary); } else if (item.type === 'fixed') { employee = new FixedSalaryEmployee(index, item.name, item.salary); } else { throw new Error('不支持的员工类型:' + item.type); } self.employees.push(employee); }); // 初始化时按规则排序 this.sortEmployees(); } // 排序方法实现 EmployeeCollection.prototype.sortEmployees = function() { this.employees.sort(function(a, b) { var salaryA = a.getSalary(); var salaryB = b.getSalary(); // 优先按薪资降序排序 if (salaryA !== salaryB) { return salaryB - salaryA; } // 薪资相同则按姓名字典序升序排序 return a.name.localeCompare(b.name); }); }; // 对外提供获取员工列表的方法 EmployeeCollection.prototype.getEmployees = function() { return this.employees; }; module.exports = EmployeeCollection;
第三步:测试调用示例
// test.js var employeeData = require('./employees-collection.json'); var EmployeeCollection = require('./EmployeeCollection.js'); var collection = new EmployeeCollection(employeeData); // 输出排序后的员工列表 console.log(collection.getEmployees());
测试输出排序结果说明
针对给定的测试JSON,最终排序顺序为:
- 固定薪资8000的Clara(与Dany薪资相同,姓名首字母排序在前)
- 固定薪资8000的Dany
- 小时工薪资1670.4的Anna
- 小时工薪资1336.32的Bob
- 固定薪资1000的Egor
内容的提问来源于stack exchange,提问作者Мария Шипова
相关产品推荐
相关产品推荐

