基础列表类
var List = function () {
var _this = this;
_this.dataStore = [];
_this.listSize = _this.dataStore.length;
_this.pos = 0;
//更新长度
_this.updateLength = function () {
_this.listSize = _this.dataStore.length;
}
//清空列表元素
_this.clear = function () {
delete _this.dataStore;
}
//字符串形式返回列表元素
_this.toString = function () {
return _this.dataStore;
}
//返回当前位置元素
_this.getElement = function () {
return _this.dataStore[_this.pos] || '超出索引';
}
//当前元素后插入新元素
_this.insert = function (element,after) {
var foundAfterAt = _this.find(after);
if(foundAfterAt > -1){
_this.dataStore.splice(foundAfterAt+1,0,element);
return true;
}
return false;
}
//列表末尾添加新元素
_this.append = function (element) {
_this.dataStore[_this.listSize] = element;
_this.updateLength();
}
//列表中删除元素
_this.remove = function (element) {
var foundAt = _this.find(element);
if(foundAt != -1){
_this.dataStore.splice(foundAt,1);
_this.updateLength();
return true;
}else{
console.log('不存在此元素');
return false;
}
}
//当前位置移动到第一个
_this.front = function () {
_this.pos = 0;
}
//当前位置移动到最后一个
_this.end = function () {
_this.pos = _this.listSize - 1;
}
//当前位置后移一位
_this.prev = function () {
if(_this.pos >= 0){
--_this.pos;
}
}
//当前位置后移一位
_this.next = function () {
if(_this.pos < _this.listSize){
++_this.pos;
}
}
//返回列表当前位置
_this.curPos = function () {
return _this.pos;
}
//当前位置移动到指定位置
_this.moveTo = function (position) {
_this.pos = position;
}
//列表中包含当前元素
_this.contains = function(element){
for(var i = 0; i < _this.listSize; i++){
if(element == _this.dataStore[i]){
return true;
}
}
return false;
}
//查找元素
_this.find = function (element) {
for(var i = 0,len = _this.listSize;i < len; i++){
if(element == _this.dataStore[i]){
return i;
}
}
return -1;
}
//获取列表长度
_this.length = function () {
return _this.listSize;
}
}
1.增加一个向列表中插入元素的方法,该方法只在待插元素大于列表中的所有元素时才执行插入操作。这里的大于有多重含义,对于数字,它是指数值上的大小;对于字母,它是指在字母表中出现的先后顺序。
//在最大元素后插入元素
List.prototype.insertAfterMax = function (element) {
var dataStore = this.dataStore;
dataStore = dataStore.sort();
var max = (typeof dataStore[0] == 'number')?dataStore[dataStore.length - 1]:dataStore[0];
this.insert(element,max);
}
var list = new List();
list.append('a');
list.append('c');
list.append('b');
list.append('e');
list.append('d');
list.append('f');
list.insertAfterMax('333')
console.log(list.toString());
2.增加一个向列表中插入元素的方法,改方法只在待插元素小于列表中的所有元素时才执行插入操作。
本题由第一题稍加修改即可
3.创建Person类,该类用于保存人的姓名和性别信息。创建一个至少包含10个Person对象的列表。写一个函数显示列表中所有拥有相同性别的人。
var Person = function (name,gender) {
this.name = name;
this.gender = gender;
}
List.prototype.getSameGender = function () {
var gender = {
man:[],
woman:[]
};
var strategy = {
'男':function (name) {
gender.man.push(name);
},
'女':function (name) {
gender.woman.push(name);
}
};
for(var i = 0; i < this.dataStore.length; i++){
var item = this.dataStore[i];
strategy[item['gender']](item['name']);
}
return gender;
}
var list = new List();
list.append(new Person('toney','男'));
list.append(new Person('toney1','男'));
list.append(new Person('toney2','男'));
list.append(new Person('toney3','男'));
list.append(new Person('toney4','男'));
list.append(new Person('linyi','女'));
list.append(new Person('linyi1','女'));
list.append(new Person('linyi2','女'));
list.append(new Person('linyi4','女'));
list.append(new Person('linyi5','女'));
list.append(new Person('linyi6','女'));
console.log(list.getSameGender());
4.修改本章的影碟租赁程序,当一部影片检出后,将其加入一个已租赁影片列表。每当有客户检出一部影片,都显示该列表中的内容。
var Movies = {
//已租列表
rentedList:new List(),
//现有列表
list:new List(),
init:function () {
this.list.dataStore = [
'《白雪公主》',
'《七个小矮人》',
'《一千零一夜》',
'《火星救援》',
];
},
checkOut:function (name) {
if(this.list.remove(name)){
this.rentedList.append(name);
console.log(this.list.dataStore);
}
}
};
Movies.init();
Movies.checkOut('《白雪公主》');
//已租列表
console.log(Movies.rentedList.dataStore);
5.为影碟租赁程序创建一个check-in()函数,当客户归还一部影片时,将该影片从已租列表中删除,同时添加到现有影片列表中。
Movies.checkIn = function (name) {
if(this.rentedList.remove(name)){
this.list.append(name);
console.log(this.list.dataStore);
}
}
Movies.checkIn('《白雪公主》');
//已租列表
console.log(Movies.rentedList.dataStore);