zoukankan      html  css  js  c++  java
  • 常用js方法

        function dateGetter(name, size, offset, trim) {
            offset = offset || 0;
            return function (date) {
                var value = date['get' + name]();
                if (offset > 0 || value > -offset)
                    value += offset;
                if (value === 0 && offset == -12) value = 12;
                return padNumber(value, size, trim);
            };
        };
        function padNumber(num, digits, trim) {
            var neg = '';
            if (num < 0) {
                neg = '-';
                num = -num;
            }
            num = '' + num;
            while (num.length < digits) num = '0' + num;
            if (trim)
                num = num.substr(num.length - digits);
            return neg + num;
        };
        function dateStrGetter(name, shortForm) {
            return function(date, formats) {
                var value = date['get' + name]();
                var get = (shortForm ? ('SHORT' + name) : name).toUpperCase();
    
                return formats[get][value];
            };
        };
        window.IGrow = {};
        var Utilities = {
            getParameterByName: function (name) {
                name = name.replace(/[[]/, "\[").replace(/[]]/, "\]");
                var regex = new RegExp("[\?&]" + name + "=([^&#]*)"),
                    results = regex.exec(location.search);
                return results === null ? "" : decodeURIComponent(results[1].replace(/+/g, " "));
            },
            findElement: function (arr, propName, propValue) {
                for (var i = 0; i < arr.length; i++)
                    if (arr[i][propName] == propValue)
                        return arr[i];
            },
            findWithAttr: function (array, attr, value) {
                for (var i = 0; i < array.length; i += 1) {
                    if (array[i][attr] === value) {
                        return i;
                    }
                }
            },
            jsonObjToBase64: function (json) {
                return btoa(encodeURIComponent(JSON.stringify(json)));
            },
            base64TojsonObj: function (base64) {
                return JSON.parse(decodeURIComponent(atob(base64)));
            },
            pageJump: function (url) {
                location.href = url;
            },
            log: function () {
                for(key in arguments){
                    console.log(JSON.parse(JSON.stringify(arguments[key])));
                }
            },
            copy: function (source) {
                var result = source instanceof Array ? [] : {};
                for (var key in source) {
                    result[key] = typeof source[key] === 'object' ? this.copy(source[key]) : source[key];
                }
                return result;
            },
            params: function () {
                var url = window.location.search;
                if (url.indexOf("?") != -1) {
                    var str = url.substr(1),
                        strs = str.split("&"),
                        key = new Array(strs.length),
                        value = new Array(strs.length),
                        params = {};
                    for (var i = 0; i < strs.length; i++) {
                        key[i] = strs[i].split("=")[0]
                        value[i] = unescape(strs[i].split("=")[1]);
                        params[key[i]] = value[i]
                    }
                    return params;
                }
            },
            getTime: function (date, format) {
                var text = '',
                    parts = [],
                    fn, match;
                while (format) {
                    match = /((?:[^yMdHhmsaZEwG']+)|(?:'(?:[^']|'')*')|(?:E+|y+|M+|d+|H+|h+|m+|s+|a|Z|G+|w+))(.*)/.exec(format);
                    if (match) {
                        parts = parts.concat([].slice.call(match, 1));
                        format = parts.pop();
                    } else {
                        parts.push(format);
                        format = null;
                    }
                }
                parts.forEach(function(value) {
                    fn = DATE_FORMATS[value];
                    text += fn ? fn(new Date(date))
                        : value.replace(/(^'|'$)/g, '').replace(/''/g, "'");
                });
                return text;
            },
            tip: function (msg,time) {
                var html = '<div class="weui_dialog_alert" id="tip">' +
                    '<div class="weui_mask"></div>' +
                    '<div class="weui_dialog">' +
                    '<div class="weui_dialog_hd"><strong class="weui_dialog_title">提示</strong></div>' +
                    '<div class="weui_dialog_bd">' + msg + '</div>' +
                    '<div class="weui_dialog_ft">', time = time || 2000;
                if ($('#tip').length) {
                    $('#tip').find('.weui_dialog_bd').html(msg);
                    $('#tip').show();
                } else {
                    $('body').append(html);
                }
                $('#tip').off().click(function(){
                    $(this).hide();
                });
                setTimeout(function () {
                    $('#tip').hide();
                }, time);
            },
            extend: function () {
                var _extend,
                    _isObject,
                    arr = arguments,
                    result = {},
                    i;
    
                _isObject = function (o) {
                    return Object.prototype.toString.call(o) === '[object Object]';
                };
    
                _extend = function self(destination, source) {
                    var property;
                    for (property in destination) {
                        if (destination.hasOwnProperty(property)) {
    
                            // 若destination[property]和sourc[property]都是对象,则递归
                            if (_isObject(destination[property]) && _isObject(source[property])) {
                                self(destination[property], source[property]);
                            }
                            ;
    
                            // 若sourc[property]已存在,则跳过
                            if (source.hasOwnProperty(property)) {
                                continue;
                            } else {
                                source[property] = destination[property];
                            }
                        }
                    }
                };
    
                if (!arr.length) return {};
    
                for (i = arr.length - 1; i >= 0; i--) {
                    if (_isObject(arr[i])) {
                        _extend(arr[i], result);
                    }
                }
    
                arr[0] = result;
    
                return result;
            },
    forEach: function (obj, iterator, context) {
    var key, length;
    if (obj) {
    if (typeof obj == 'function') {
    for (key in obj) {
    // Need to check if hasOwnProperty exists,
    // as on IE8 the result of querySelectorAll is an object without a hasOwnProperty function
    if (key != 'prototype' && key != 'length' && key != 'name' && (!obj.hasOwnProperty || obj.hasOwnProperty(key))) {
    iterator.call(context, obj[key], key, obj);
    }
    }
    } else if ($.isArray(obj)) {
    var isPrimitive = typeof obj !== 'object';
    for (key = 0, length = obj.length; key < length; key++) {
    if (isPrimitive || key in obj) {
    iterator.call(context, obj[key], key, obj);
    }
    }
    } else if (obj.forEach && obj.forEach !== Utilities.forEach) {
    obj.forEach(iterator, context, obj);
    } else {
    for (key in obj) {
    if (obj.hasOwnProperty(key)) {
    iterator.call(context, obj[key], key, obj);
    }
    }
    }
    }
    return obj;
    }

    }; Utilities.routeParams = Utilities.params();

     常用js方法,extend合并对象,copy深拷贝对象,tip提示信息框,getTime返回自定义格式时间,params获取浏览器地址参数,log打印相关变量,forEach循环对象或数组

  • 相关阅读:
    Java-API-Package:java.sql百科
    Java-API-Package:java.net百科
    Java-API-Package:java.lang
    Java-API:java.lang百科
    Java-API-Package:org.springframework.stereotype
    Java-API-Package:org.springframework.beans.factory.annotation
    Java-API-Package:org.springframwork.transaction.annotation
    Java-API-Package:org.springframework.web.bind.annotation
    Java-API-Package:javax.annotation
    Java-API-Package:java.math
  • 原文地址:https://www.cnblogs.com/yu-hao/p/6140010.html
Copyright © 2011-2022 走看看