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;
  • 相关阅读:
    luogu 2742 二维凸包
    poj2398 Toy Storage 计算几何,叉积,二分
    luoguP1742 最小圆覆盖
    bzoj4501 旅行
    cf1173 D. Nauuo and Circle
    bzoj3745: [Coci2015]Norma 分治,单调队列
    bzoj1176: [Balkan2007]Mokia cdq
    luoguP3964 [TJOI2013]松鼠聚会
    浅谈数学期望
    tarjan模板(带注释)
  • 原文地址:https://www.cnblogs.com/bluen/p/5480533.html
Copyright © 2011-2022 走看看