为了账号安全,请及时绑定邮箱和手机立即绑定

一个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前端工程师
手记
粉丝
9110
获赞与收藏
2572

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消