js实用工具方法及小技巧

来源:互联网 发布:算法导论22.3 13 编辑:程序博客网 时间:2024/06/08 11:45

分享一些自己在开发过程中实用的工具方法

/** * @param format {String} 传入要格式化日期的格式  * @return    {String} 返回:格式化之后的日期 * (new Date()).pattern("yyyy-MM-dd hh:mm:ss") ==> 2017-10-18  08:09:04 * (new Date()).pattern("yyyy年MM月dd日") ==>2017年10月18日 * (new Date()).pattern("MM/dd/yyyy") ==> 10/18/2017 * (new Date()).pattern("yyyyMMdd") ==> 20171018 */ Date.prototype.pattern = function (format){      var o = {          "M+" : this.getMonth()+1, //month          "d+" : this.getDate(), //day          "h+" : this.getHours(), //hour          "m+" : this.getMinutes(), //minute          "s+" : this.getSeconds(), //second          "q+" : Math.floor((this.getMonth()+3)/3), //quarter          "S" : this.getMilliseconds() //millisecond      }      if(/(y+)/.test(format)) {          format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));      }      for(var k in o) {          if(new RegExp("("+ k +")").test(format)) {              format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));          }      }      return format;  }  
/** * @param tag {String} 传入一个字符串标记 * @return    {String} 返回:page12343433243 */~function (W) {    var star = 1111, timeStap = (new Date()).getTime();    W.getID = function (tag) {        tag += '';        return tag + timeStap + star++;    }}(window)
/** * @param na  {String} 传入要查询的key * @param url {String} 传入url地址 * @return    {String} 返回对应的value */function getQuery(na, url) {    let r = url.match(new RegExp('(^|&|\\?)' + na + '=([^&]*)(&|$)'));    if (r != null)        return unescape(r[2]);    return false;}
/** * @param o  {任意类型} 传入要查询的值 * @return    {Boolean} 返回true/false */~ function(W) {        var gettype = Object.prototype.toString;        W.utility = {            isObj: function(o) {                return gettype.call(o) == "[object Object]";            },            isArray: function(o) {                return gettype.call(o) == "[object Array]";            },            isNull: function(o) {                return gettype.call(o) == "[object Null]";            },            isString: function(o){                return gettype.call(o) == '[object String]';            },            isNumber: function(o){                return gettype.call(o) == '[object Number]';            },            isBoolean: function(o){                return gettype.call(o) == '[object Boolean]';            },            isUndefined: function(o){                return gettype.call(o) == '[object Undefined]';            }        }    }(window)
/** * 字符串操作工具类 */var strUtil = {      /*      * 判断字符串是否为空      * @param str 传入的字符串      * @returns {Boolean}      */      isEmpty:function(str){          if(str != null && str.length > 0){              return true;          }else{              return false;          }      },      /*      * 判断两个字符串子否相同      * @param str1      * @param str2      * @returns {Boolean}      */      isEquals:function(str1,str2){          if(str1==str2){              return true;          }else{              return false;          }      },      /*      * 忽略大小写判断字符串是否相同      * @param str1      * @param str2      * @returns {Boolean}      */      isEqualsIgnorecase:function(str1,str2){          if(str1.toUpperCase() == str2.toUpperCase()){              return true;          }else{              return false;          }      },       /**      * 判断是否是中文      * @param str      * @returns {Boolean}      */      isChine:function(str){          var reg = /^([u4E00-u9FA5]|[uFE30-uFFA0])*$/;          if(reg.test(str)){              return false;          }          return true;      }  };  
 /**      * 随机生成一段范围内的整数     * @param min 最小值     * @param max 最大值     * @returns {Number}      */   function setRadomNum(min,max){    return  Math.floor(Math.random() * (max - min + 1)) + min; }
/**  * 数组深复制 */    var a = [1,2]    var b = a.slice();    b[0] = 33;  //[33,2]    console.log(a) // [1,2]
/**  * json转url参数 * @param json {Object} * @returns {String}   * {name:'tc',age:22} => name=tc&age=22 */function json2url(json) {        var arr=[];        for(var name in json){            arr.push(name+'='+json[name]);        }        return arr.join('&');    }
/**  * 打乱数组顺序 * @param arr {Array} * @returns {Array}  返回打乱之后的数组 */function arrorder(arr) {       return arr.sort(function(){return Math.random() - 0.5});  }
原创粉丝点击