遍历列表

    function List() {
        this.listSize = 0;//列表的元素个数,属性
        this.pos = 0;//列表的当前位置,属性
        this.dataStore = []; // 初始化一个空数组来保存列表元素

        this.append = append;//在列表的末尾添加新元素,方法

        this.front = front;//将列表的当前位置设移动到第一个元素,方法
        this.end = end;//将列表的当前位置移动到最后一个元素,方法
        this.prev = prev;//将当前位置前移一位,方法
        this.next = next;//将当前位置后移一位,方法
        this.moveTo = moveTo;//将当前位置移动到指定位置,方法

        this.currPos = currPos;//返回列表的当前位置,方法    
        this.getElement = getElement;//返回当前位置的元素,方法
    }
    function append(element) {
        this.dataStore[this.listSize++] = element;
        //后自加,在新位置添加元素,同时列表的元素个数加1
    }

    function front() {
        this.pos = 0;
    }
    function end() {
        this.pos = this.listSize - 1;
    }
    function prev() {
        if (this.pos > 0) {
            --this.pos;
        }
    }
    function next() {
        if (this.pos < this.listSize - 1) {
            ++this.pos;
        }
    }
    function moveTo(position) {
        this.pos = position;
    }

    function currPos() {
        return this.pos;
    }
    function getElement() {
        return this.dataStore[this.pos];
    }

    var names = new List();
    names.append("Clayton");
    names.append("Raymond");
    names.append("Cynthia");
    names.front();
    alert(names.getElement()); // 显示 Clayton
原文地址:https://www.cnblogs.com/feile/p/5371171.html