自己定义的一些js方法,一直更新中

/**
 * time:2017-10-11
 * name:cst
 */
var chu=(function(){
    'use strict'
    var classType = {},emptyArray=[],
        toString = classType.toString,
        filter=emptyArray.filter;
    "Boolean Number String Function Array Date RegExp Object Error".split(" ").forEach(function(value,index,arr){
        classType[ "[object " + value + "]" ] = value.toLowerCase();
    });
    var chu={
        //获取url输入框中的参数
        getUrlField:function(name){
            var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
            var r = window.location.search.substr(1).match(reg);
            if (r != null) {
                return decodeURI(r[2]);
            }
            return null;
        },
        //判断是否是数组 isArray(arr)
        isArray:Array.isArray || function(arr){ return arr instanceof Array;},
        //是否是函数
        isFunction:function(value){return typeof value === 'function';},
        //判断是否为window 对象  window===window.window
        isWindow:function(value){return value!=null && value.window===value; },
        //判断是够是 document 
        //window.document.nodeType == 9 数字表示为9,常量表示为 DOCUMENT_NODE 
        isDocument:function(obj){return obj != null && obj.nodeType == obj.DOCUMENT_NODE; },
        // 判断是否是object 对象
        isObject:function(obj){return typeof obj === 'object';},
        //判断是否是最原始的object 对象
        // Object.getPrototypeOf(obj) === Object.prototype;
        isPlainObject:function(obj){
            return this.isObject(obj) && !this.isWindow(obj) && Object.getPrototypeOf(obj) === Object.prototype;
        },
        //判断类型
        type:function(obj){
            return obj === null?String(obj):classType[toString.call(obj)]||'object';
        },
        //过滤数组中的null undefined
        compact:function(arr){
            return filter.call(arr,function(value,index,arr){
                return value != null;
            });
        },
        //数组去重
        uniq:function(arr){
            return filter.call(arr,function(value,index){
                return arr.indexOf(value) === index;
            });
        },
        //判断是否是空对象
        isEmptyObj:function(obj){
            for(var key in obj){
                return false;
            }
            return true;
        },
        //深度赋值 对象
        // deep true 深度赋值
        deepExtend:function(target,source,deep){
            for(var key in source){
                if( deep && (this.isPlainObject(source[key])) || this.isArray(source[key]) ){
                    if( this.isArray(source[key]) ){
                        target[key]=[];
                    }
                    if( this.isPlainObject(source[key]) ){
                        target[key]={};
                    }
                    this.deepExtend(target[key],source[key],deep);
                }else if( source[key] !== undefined ){
                    target[key] = source[key];
                }
            }
        },
        //深度合并  对象
        // deep true  深度
        deepMerge:function(target,source,deep){
            for( var key in source ){
                if( deep&&( this.isPlainObject(source[key]) ) || !this.isArray(source[key]) ){
                    if( this.isPlainObject(source[key]) && this.isPlainObject(target[key]) ){
                        target[key] = {};
                    }
                    if( this.isArray(source[key]) || !this.isArray(target[key]) ){
                        target[key] = [];
                    }
                    this.deepMerge(target[key],source[key],deep);
                }else if( source[key] !== undefined ){
                    target[key] = source[key];
                }
            }
        },
        //判断是否包含子元素
        contains:document.documentElement.contains?//判断浏览器是够支持contains 方法
            function(parent,node){
                return parent !== node && parent.contains(node);
            }:
            function(parent,node){
                while(node){
                    node=node.parent;
                    if( node === parent ){
                        return true;
                    }
                }
                return false;
            }
    }
    return chu;
})();

 

posted @ 2017-10-12 10:29  楚歌忄  阅读(269)  评论(0)    收藏  举报