js的一些工具方法

2017-03-10  本文已影响0人  柏衡
[obj判断类型:返回传递给他的任意对象的类]
    isClass:function(o){
        if(o===null) return "Null";
        if(o===undefined) return "Undefined";
        return Object.prototype.toString.call(o).slice(8,-1);
    },
[数组及对象深拷贝]
var arr = [1,'2',{a:1,b:[1,2]}];
function deepCopy(p, c) {    
    var c = c || {};    
    for (var i in p) {      
        if (typeof p[i] === 'object' && p[i] !== null) {        
            c[i] = (p[i].constructor === Array) ? [] : {};        
            deepCopy(p[i], c[i]);      
        } else {         
            c[i] = p[i];      
        }    
    }    
    return c;  
}
var cArr = deepCopy(arr);
console.log(cArr);
[列表排序:按元素的key]
ArraySortKey:function(key,desc) {
        desc = desc || false;
        return function(a,b){
            return desc ? ~~(a[key] < b[key]) : ~~(a[key] > b[key]);
        }
    },
[console override]
    to:function(){
       console.log.apply(console, arguments);
    }
[伪DOM Events]
export default {
    listenterList:[],
    events:{
        test:"test",
    },
    addEvent: function(type, fn) {
        if (typeof this.listenterList[type] === "undefined") {
            this.listenterList[type] = [];
        }
        if (typeof fn === "function") {
            this.listenterList[type].push(fn);
        }
        return this;
    },
    triggerEvent: function(type, _arg) {
        var arrayEvent = this.listenterList[type];
        if (arrayEvent instanceof Array) {
            for (var i = 0; i < arrayEvent.length; i++) {
                if (typeof arrayEvent[i] === "function") {
                    arrayEvent[i].apply(this, [_arg]);
                }
            }
        }
    },
    removeEvent: function(type, fn) {
        var arrayEvent = this.listenterList[type];
        if (typeof type == "string" && arrayEvent instanceof Array) {
            if (typeof fn === "function") {
                for (var i = arrayEvent.length - 1; i >= 0; i--) {
                    if (arrayEvent[i] === fn) {
                        this.listenterList[type].splice(i, 1);
                        // console.log("removeEvent success:", arrayEvent, fn)
                        break;
                    }
                }
            } else {
                delete this.listenterList[type];
            }
        }
        return this;
    },
    removeEvents: function() {
        for (var type in this.listenterList) {
            this.removeEvent(type);
        }
    }
}
[reg验证]
    reg_url:/^(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/,//url
    reg_email:/\w+[@]{1}\w+[.]\w+/,//email
    reg_ip:/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/,//ip
    regMatches:function(_reg, _str){
        if(_reg.test(_str)){
            return true;
        }else{
            return false;
        }
    }
[分页计算]
    paging:function(_currpage, _totalPage, _amount){
        var _min = _currpage-parseInt(_amount/2);
        if(_min<1) _min = 1;
        var _max = _min+_amount-1;
        if(_max>_totalPage) _max = _totalPage;
        
        var _pageNumberlist = [];
        for(var i=_min; i<=_max; i++){
            _pageNumberlist.push(i);
        }

        var _obj = {}
        _obj.page = _currpage;//当前页
        _obj.pages = _totalPage;//总页数
        _obj.pageMin = _min;//最小显示页
        _obj.pageMax = _max;//最大显示页
        _obj.numberlist = _pageNumberlist;//显示页码列表
        return _obj;
    }
[获取距今天dayCount天后的日期 "yy-mm-dd"]
    getDateStr: function(dayCount) {
        var dd = new Date();
        dd.setDate(dd.getDate() + dayCount);
        var y = dd.getFullYear();
        var m = dd.getMonth() + 1;
        var d = dd.getDate();
        return y + "-" + m + "-" + d;
    },
["yy-mm-dd"转日期]
    getDate_byStr: function(_str) {
        return new Date(Date.parse(_str.replace(/-/g, "/")));
    },
上一篇下一篇

猜你喜欢

热点阅读