JS设计模式:迭代器模式

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 {
    f<a href="https://www.jb51.cc/tag/irs/" target="_blank" class="keywords">irs</a>t : 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);
        }
    }
}

}

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

相关文章

显卡天梯图2024最新版,显卡是电脑进行图形处理的重要设备,...
初始化电脑时出现问题怎么办,可以使用win系统的安装介质,连...
todesk远程开机怎么设置,两台电脑要在同一局域网内,然后需...
油猴谷歌插件怎么安装,可以通过谷歌应用商店进行安装,需要...
虚拟内存这个名词想必很多人都听说过,我们在使用电脑的时候...