java cookie操作的封装 常用判断方法的封装详解编程语言

'use strict';//启用严格模式 
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); 
            } 
        } 
    }; 
var _cookie = { 
        set:function(name, value){ 
            var expires = (arguments.length > 2) ? arguments[2] : null,strExpires = ""; 
            if(expires && oap.isDate(expires)){ 
                strExpires =  "; expires=" + expires.toGMTString(); 
            }else if(expires && oap.isObj(expires)){ 
                var nD = oap.now(), 
                    nObj = { 
                        day:0,hours:0,minutes:0,seconds:0 
                    }, 
                    dArr={ 
                        secondes:1000, 
                        minutes:1000*60, 
                        hours:1000*60*60, 
                        day:1000*60*60*24 
                    }, 
                    _val; 
                nObj = oap.extend(nObj,expires); 
                for(var n in expires){ 
                    _val = nObj[n]; 
                    if(oap.isNum(_val) && _val>0){ 
                        nD = nD + _val * dArr[n]; 
                    } 
                } 
                if(oap.isNum(nD) && nD>0){ 
                    nD = new Date(nD); 
                    strExpires =  "; expires=" + nD.toGMTString(); 
                } 
            }else{ 
                strExpires = ""; 
            } 
 
            document.cookie = name + "=" + encodeURIComponent(value) + strExpires + ";path=/" ; 
        }, 
        get:function(name){ 
            var value = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)")); 
            if (value != null) { 
                return decodeURIComponent(value[2]); 
            } else { 
                return null; 
            } 
        }, 
        remove:function(name){ 
            var expires = new Date(); 
            expires.setTime(expires.getTime() - 1000 * 60); 
            this.set(name, "", expires); 
        } 
    }; 
 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/10422.html

(0)
上一篇 2021年7月19日 10:13
下一篇 2021年7月19日 10:13

相关推荐

发表回复

登录后才能评论