示例参考 - ES5
<script>
function Student(termId) {
this.termId = termId;
}
function StudentList() {
this.list = [];
}
StudentList.prototype.add = function (student) {
this.list.push(student);
}
StudentList.prototype.remove = function (index) {
this.list.splice(index, 1);
}
StudentList.prototype.removeByTerm = function (termId) {
this.list = this.list.filter(function (student) {
return student.termId != termId;
});
}
var list = new StudentList;
list.add(new Student(1));
list.add(new Student(1));
list.add(new Student(2));
list.add(new Student(2));
console.log(list);
list.removeByTerm(2);
console.log(list);
</script>
示例参考 - ES6
<script>
class Student {
constructor(termId) {
this.termId = termId;
}
}
class StudentList {
constructor() {
this.list = [];
}
add(student) {
this.list.push(student);
}
removeByTerm(termId) {
this.list = this.list.filter(function (student) {
return student.termId != termId;
});
}
}
const list = new StudentList;
list.add(new Student(1));
list.add(new Student(1));
list.add(new Student(2));
list.add(new Student(2));
console.log(list);
list.removeByTerm(2);
console.log(list);
</script>
登录后评论