对象合并 js自己有这个方法
1 Object .assign (target, ...sources)
这种会新添和覆盖属性,如果我们只想覆盖原有属性不新添
1 2 3 4 5 6 7 8 export const my_assign = function (target, source ) { let keys = Object .keys (source); for (const key of keys) { if (target.hasOwnProperty (key)) { target[key] = source[key]; } } }
定义对象
字符串转json对象 1 2 3 4 5 6 7 8 9 10 zj.toJson = function (result ) { if (typeof result == "string" ) { return eval ("(" + result + ")" ); } else { return result; } }
格式化字符串 1 2 3 4 5 6 7 8 9 10 11 zj.formatString = function (str ) { for (var i = 0 ; i < arguments .length - 1 ; i++) { str = str.replace ("{" + i + "}" , arguments [i + 1 ]); } return str; };
字符串转数组 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 zj.stringToList = function (value ) { if (typeof value == "object" ) { return value; } else if (value != undefined && value != '' ) { var values = []; console .info (value); var t = value.split (',' ); for (var i = 0 ; i < t.length ; i++) { values.push ('' + t[i]); } console .info (values); return values; } else { return []; } };
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 zj.clearForm = function (form ) { $(':input' , form).each (function ( ) { var type = this .type ; var tag = this .tagName .toLowerCase (); if (type == 'text' || type == 'password' || tag == 'textarea' ) { this .value = "" ; } else if (type == 'checkbox' || type == 'radio' ) { this .checked = false ; } else if (tag == 'select' ) { this .selectedIndex = -1 ; } }); };
日期格式化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 Date .prototype .format = function (format ) { var o = { "M+" : this .getMonth () + 1 , "d+" : this .getDate (), "h+" : this .getHours (), "m+" : this .getMinutes (), "s+" : this .getSeconds (), "q+" : Math .floor ((this .getMonth () + 3 ) / 3 ), "S" : this .getMilliseconds () } 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; }
隐藏域中进行同步get提交 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 let iframeGet = function (url ) { var nowDate = new Date ().getSeconds () + "" ; var iframe; try { iframe = document .createElement ('<iframe name="' + nowDate + '">' ); } catch (ex) { iframe = document .createElement ("iframe" ); } iframe.name = nowDate; iframe.width = 0 ; iframe.height = 0 ; iframe.marginHeight = 0 ; iframe.marginWidth = 0 ; document .body .appendChild (iframe); iframe.src = url; };
js进行post同步提交 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 zj.post = function (URL, PARAMS ) { var temp = document .createElement ("form" ); temp.action = URL ; temp.method = "post" ; temp.style .display = "none" ; for ( var x in PARAMS ) { var opt = document .createElement ("textarea" ); opt.name = x; opt.value = PARAMS [x]; temp.appendChild (opt); } document .body .appendChild (temp); temp.submit (); }
获取字符串的分隔符之前部分 1 2 3 4 5 6 zj.getLeftStr = function (str, delimiter ) { return str.substring (0 , str.indexOf (delimiter)); }
获取浏览器信息 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 zj.getBoswerMsg = function ( ) { var Sys = {}; var ua = navigator.userAgent .toLowerCase (); var s; var scan; (s = ua.match (/msie ([\d.]+)/ )) ? Sys .ie = s[1 ] : (s = ua .match (/firefox\/([\d.]+)/ )) ? Sys .firefox = s[1 ] : (s = ua .match (/chrome\/([\d.]+)/ )) ? Sys .chrome = s[1 ] : (s = ua .match (/opera.([\d.]+)/ )) ? Sys .opera = s[1 ] : (s = ua .match (/version\/([\d.]+).*safari/ )) ? Sys .safari = s[1 ] : 0 ; if (Sys .ie ) { scan = { "bowser" : "ie" , "version" : zj.getLeftStr (Sys .ie , "." ) }; } if (Sys .firefox ) { scan = { "bowser" : "firefox" , "version" : zj.getLeftStr (Sys .firefox , "." ) }; } if (Sys .chrome ) { scan = { "bowser" : "chrome" , "version" : zj.getLeftStr (Sys .chrome , "." ) }; } if (Sys .opera ) { scan = { "bowser" : "opera" , "version" : zj.getLeftStr (Sys .opera , "." ) }; } if (Sys .safari ) { scan = { "bowser" : "safari" , "version" : zj.getLeftStr (Sys .safari , "." ) }; } console .info (scan); return scan; }
格式化小数 1 2 3 zj.formatFloat = function (num, pos ) { return Math .round (num * Math .pow (10 , pos)) / Math .pow (10 , pos); }