zoukankan      html  css  js  c++  java
  • 【全站工程师练习笔记】Web前端 自定义JS对象 object_list

        function object_list(){
            return new object_list.fn.init(arguments);
        }
        object_list.fn = object_list.prototype = {
            constructor:object_list,
            init:function(args){
                this.useDefaultList = true;
                this.object = {};
                return this;
            }
        };
        object_list.fn.init.prototype = object_list.fn;
        object_list.prototype.clear = function(){
            var k = arguments[0];
            if (k && this.has(k)) {delete this.object[k];}
            else { this.object = {};}
        }
        object_list.prototype.push = function (k,v) {
            if (this.useDefaultList){
                if (!this.object[k]){
                    this.object[k] = new Array();
                }
                this.object[k].push(v);
            }else{
                if (!this.object[k]){
                    this.object[k] = v;
                }else{
                    var l = this.object[k];
                    var arr = new Array();
                    arr.push(l);
                    arr.push(v);
                    this.object[k] = arr;
                }
            }
            return this;
        };
        object_list.prototype.has =function(k){ return this.object[k] != undefined;};
        object_list.prototype.get = function(k){
            return this.object[k];
        };
        object_list.prototype.safe_get_call = function (k,target,_func_) {
            if (this.has(k)){
                _func_.call(this,k,this.get(k));
            }
        };
        object_list.prototype.get_with_default = function (k,d) {
            return this.has(k)?this.get(k):d;
        };
        object_list.prototype.for = function () {
            var f = arguments[0];
            var rstr= arguments[1] != undefined?arguments[1]:"";
            var r = (r.toLowerCase().indexOf("r") != -1 )? true:false;
            for (var k in this.object){
                if (!r){
                    f(k,0,this.object[k]);
                }else{
                    if (is_array(this.object[k])){
                        for (var j in this.object[k]){
                            f(k,j,this.object[k][j]);
                        }
                    }else{
                        f(k,0,this.object[k]);
                    }
                }
            }
        };
        object_list.prototype.for_with_target = function () {
            var f = arguments[0];
            var target = arguments[1];
            var rstr= arguments[2] != undefined?arguments[2]:"";
            var r = (r.toLowerCase().indexOf("r") != -1 )? true:false;
            for (var k in this.object){
                if (!r){
                    f(k,0,this.object[k]);
                }else{
                    if (is_array(this.object[k])){
                        for (var j in this.object[k]){
                            f.call(target,k,j,this.object[k][j]);
                        }
                    }else{
                        f.call(target,k,0,this.object[k]);
                    }
                }
            }
        };
        win.object_list = object_list;
  • 相关阅读:
    IT战略规划项目方法论(转)
    008_Node中的require和import
    007_Mac上安装Node和NPM
    005_讨论多线程和单线程
    006_饿了么大前端总监sofish帮你理清前端工程师及大前端团队的成长问题!
    005_解密饿了么大前端团队
    005_python对整数的拼接
    010_动态语言与鸭子类型及python2和3的区别
    010_vim和python整合开发
    009_一行python重要工具
  • 原文地址:https://www.cnblogs.com/bluen/p/5480533.html
Copyright © 2011-2022 走看看