一些常用的JS方法

var oap={},obj_type={},core_toString=obj_type.toString,hasOwn = obj_type.hasOwnProperty;;
    var type_arr = "Boolean Number String Function Array Date RegExp Object Error".split(" ");
    for(var i in type_arr){
        obj_type[ "[object " + type_arr[i] + "]" ] = type_arr[i].toLowerCase();
    };

    //常用工具  判断方法 对象
    oap={
        type:function(obj){
            if ( obj == null ) {
                return String( obj );
            }
            return (typeof obj === "object" || typeof obj === "function")?(obj_type[ core_toString.call(obj) ] || "object") :typeof obj;
        },
        isStr:function(obj){
            return oap.type(obj)==="string";
        },
        isFn:function(obj){
            return oap.type(obj)==="function";
        },
        isObj:function(obj){
            return oap.type(obj) === "object";
        },
        isDate:function(obj){
            return oap.type(obj) === "date";
        },
        isEmptyObj:function(obj){
            var name;
            for ( name in obj ) {
                return false;
            }
            return true;
        },
        isNum:function(obj){
            return !isNaN( parseFloat(obj) ) && isFinite( obj );
        },
        isArr:function(obj){
            return oap.type(obj) === "array";
        },
        trim:function(obj){
            return obj.replace(/^\s+|\s+$/g, "");
        },
        now:function(){
            return new Date().getTime();
        },
        log:function(str){
            if(console && console.log){
                console.log(str);
            }
        }
    };


自己写的一段代码,主要是一些常用的判断方法,其中的type是模仿了jquery的。

保持一颗好奇的心
原文地址:https://www.cnblogs.com/subying/p/3063192.html