亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

一個js列表實現。

列表简介:下面分享的这个列表是我精心设计的,里面融和了很多js的设计模式,有需要的朋友可以看一看。
/**

  • Created by MAORUIBIN on 2016-03-16.
    */
    (function(window) {
    var win = window,
    _restore = {
    listSize: function() {
    return this.restore.length;
    },
    length: function() {
    console.log(this.restore);
    return this.restore.length;
    },
    clear: function() {
    delete this.restore;
    this.restore = [];
    this.listIndex = 0;
    return this;
    },
    toSting: function() {
    return this.restore;
    },
    getElement: function(ele) {
    var index = this.find(ele);
    if (index > -1) {
    return this.restore[index];
    }
    return null;
    },
    insert: function(ele, pos) {
    this.listIndex++;
    this.restore.splice(pos, 0, ele);
    return this;
    },
    append: function(ele) {
    this.listIndex++;
    this.restore.push(ele);
    return this;
    },
    remove: function(ele) {
    this.listIndex--;
    var index = this.find(ele);
    this.restore.splice(index, 1);
    return this;
    },
    front: function(ele) {
    var index = this.find(ele);
    this.restore.splice(index, 1);
    this.restore.splice(0, 0, ele);
    return this;
    },
    end: function(ele) {
    var index = this.find(ele);
    this.restore.splice(index, 1);
    this.restore.splice(this.restore.length, 0, ele);
    return this;
    },
    prev: function(ele) {
    var index = this.find(ele);
    this.restore.splice(index, 1);
    this.restore.splice(index-1, 0, ele);
    return this;
    },
    next: function(ele) {
    var index = this.find(ele);
    console.log(index);
    this.restore.splice(index,1);
    this.restore.splice(index + 1, 0, ele);
    return this;
    },
    currPos: function(ele) {
    return this.find(ele);
    },
    moveTo: function(ele, pos) {
    var index = this.find(ele);
    this.restore.splice(index,1);
    this.restore.splice(pos, 0, ele)
    return this;
    },
    find: function(ele) {
    for (var i = 0, len = this.restore.length; i < len; ++i) {
    if (ele === this.restore[i]) {
    return i;
    }
    }
    return -1;
    }
    }
    function list() {
    return new list.prototype.init();
    }
    list.fn = list.prototype ={
    init: function() {
    this.pos = 0;
    this.listIndex = 0;
    this.restore = [];
    this.listSize = _restore.listSize;
    this.length = _restore.length;
    this.clear = _restore.clear;
    this.toString = _restore.toSting;
    this.getElement = _restore.getElement;
    this.insert = _restore.insert;
    this.append = _restore.append;
    this.remove = _restore.remove;
    this.front = _restore.front;
    this.end = _restore.end;
    this.prev = _restore.prev;
    this.next = _restore.next;
    this.currPos = _restore.currPos;
    this.moveTo = _restore.moveTo;
    this.find = _restore.find;
    this._sort = _restore.sort;
    }
    };
    list.fn.init.prototype = list.fn;
    win.$ = win.list = list;
    win.$$ = list();
    })(window)
點擊查看更多內容
3人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
Web前端工程師
手記
粉絲
9109
獲贊與收藏
2572

關注作者,訂閱最新文章

閱讀免費教程

感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消