示例参考 - ES5
<script>
function Teacher(lessonPlan) {
this.fullAttendence = true;
this.lessonPlan = lessonPlan;
this.absentPlan = [[], [], [], [], [],];
this.additionalPlan = [[], [], [], [], [],];
}
function ReassignTeacherCommand(t1, t2, day) {
this.t1 = t1;
this.t2 = t2;
this.day = day;
}
ReassignTeacherCommand.prototype.do = function () {
this.t2.additionalPlan[this.day] =
this.t1.absentPlan[this.day] =
this.t1.lessonPlan[this.day];
}
ReassignTeacherCommand.prototype.undo = function () {
this.t1.absentPlan[this.day] = [];
this.t2.additionalPlan[this.day] = [];
}
function LeaveCommand(teacher, replacer, day) {
this.teacher = teacher;
this.reassign = null;
this.replacer = replacer;
this.day = day;
}
LeaveCommand.prototype.do = function () {
this.teacher.fullAttendence = false;
console.log('请假成功');
this.reassign = new ReassignTeacherCommand(this.teacher, this.replacer, this.day);
this.reassign.do();
}
LeaveCommand.prototype.undo = function () {
this.teacher.fullAttendence = true;
console.log('撤销请假');
this.reassign.undo();
}
var whhPlan = [
['09:30'],
['09:30', '14:30'],
[],
[],
['15:00'],
];
var lsdPlan = [
['09:30'],
[],
[],
[],
['15:00'],
];
var whh = new Teacher(whhPlan);
var lsd = new Teacher(lsdPlan);
var leave = new LeaveCommand(whh, lsd, 1);
leave.do();
console.log('whh:', whh)
console.log('lsd:', lsd)
leave.undo();
console.log('whh:', whh)
console.log('lsd:', lsd)
</script>
登录后评论