加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > C语言 > 正文

JS设计模式:迭代器模式

发布时间:2020-12-15 00:30:41 所属栏目:C语言 来源:网络整理
导读:var Iterator = function(items,container) { var container = container document.getElementById(container) || document,items = container.getElementsByTagName(items),length = items.length,index = 0; var splice = [].splice; return { first : fun
var Iterator = function(items,container) {
    var container = container && document.getElementById(container) || document,items = container.getElementsByTagName(items),length = items.length,index = 0;
    var splice = [].splice;
return {
    first : function() {
        index = 0;
        return items[index];
    },second : function() {
        index = length - 1;
        return items[index];
    },pre : function() {
        if (--index > 0) {
            return items[index];
        } else {
            index = 0;
            return null;
        }
    },next : function() {
        if (++index < length) {
            return items[index];
        } else {
            index = length - 1;
            return null;
        }
    },get : function() {
        index = num >= 0 ? num % length : num % length + length;
        return items[index];
    },dealEach : function(fn) {
        var args = splice.call(arguments,1);
        for (var i = 0; i < length; i++) {
            fn.apply(items[i],args);
        }
    },dealItem : function(num,fn) {
        fn.apply(this.get(num),splice.call(arguments,2));
    },exclusive : function(num,allFn,numFn) {
        this.dealEach(allFn);
        if (Object.prototype.toString(items[num]) === "[object Array]") {
            for (var i = 0,len = num.length; i < len; i++) {
                this.dealItem(num[i],numFn);
            }
        } else {
            this.dealItem(num,numFn);
        }
    }
}

}

操作一个聚合对象中的每一个元素的方法集合。 (状态模式 策略模式)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读