本文共 1014 字,大约阅读时间需要 3 分钟。
function List () { // 初始化 this.dataStore = []; this.listSize = 0; this.pos = 0; // 当前的位置,默认为0 this.length = length; // 返回列表的长度 this.clear = clear; // 清空列表元素 this.getCurrElement = getCurrElement; // 获取当前元素 this.insert = insert; // 在某一元素的后面插入一个元素 this.append = append; // 在列表末尾添加新元素 this.find = find; // 这个是辅助方法,用于查找列表中有无某个元素 this.toString = toString; // 返回当前的列表,查看元素 } function find (ele) { for (let i = 0;i-1) { this.dataStore.splice(pos + 1, 0, after); this.listSize++; // 插入元素之后,列表长度加一 return true; } return false; } function toString () { return this.dataStore.toString(); } const list = new List(); list.append(1); list.append(2); console.log(list.length()); list.insert(1, 121); console.log(list.toString());
转载地址:http://ygthn.baihongyu.com/