String.prototype.isNullOrWhiteSpace = function () { /// <summary> /// 变量是undefined或者null或者空字符串 出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢! /// </summary> /// <returns type="Boolean" /> if (this.trim() == "") { return true; } else if (this == null) { return true; } else { return false; } } String.prototype.trim = function () { /// <summary> /// 去除左右所有空格 /// </summary> /// <returns type="String" /> return this.replace(/(^\s*)|(\s*$)/g, ""); } String.prototype.ltrim = function () { /// <summary> /// 去除左所有空格 出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢! /// </summary> /// <returns type="String" /> return this.replace(/(^\s*)/g, ""); } String.prototype.rtrim = function () { /// <summary> /// 去除右所有空格 /// </summary> /// <returns type="String" /> return this.replace(/(\s*$)/g, ""); } String.prototype.isNumber = function () { /// <summary> /// 是否是数字 /// </summary> /// <returns type="Boolean" /> return this != "" && !isNaN(this); } Number.prototype.isNumber = function () { /// <summary> /// 是否是数字 出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢! /// </summary> /// <returns type="Boolean" /> return this.toString().isNumber(); } String.prototype.toFix = function (fix, isComplement) { /// <summary> /// 去掉多余小数并且不四舍五入 /// </summary> /// <param name="fix" type="int"> /// 要保留的小数位数 出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢! /// </param> /// <param name="isComplement" type="string"> /// 是否补位 true false /// </param> /// <returns type="Number" /> if (this.isNumber() && fix.isNumber()) { var array = this.split('.'); if (array.length > 1 && fix.toString().split('.').length == 1) { if (fix <= 0) { return Number(array[0]); } if (isComplement == "true") { while (array[1].length < fix) { array[1] += "0"; } return array[0] + "." + array[1].substring(0, fix); } return Number(array[0] + "." + array[1].substring(0, fix)); } else { if (isComplement == "true") { var point = ""; while (point.length < fix) { point += "0"; } return this.toString() + "." + point; } } } return Number(this); } Number.prototype.toFix = function (fix, isComplement) { /// <summary> /// 去掉多余小数并且不四舍五入 /// </summary> /// <param name="fix" type="int"> /// 要保留的小数位数 /// </param> /// <param name="isComplement" type="string"> /// 是否补位 true false /// </param> /// <returns type="Number" /> return this.toString().toFix(fix.toString(), isComplement); } Number.prototype.fAdd = function (arg2) { /// <summary> /// 浮点加法运算(解决JS浮点运算的bug) /// </summary> /// <param name="arg2" type="int"> /// 右表达式 出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢! /// </param> /// <returns type="Number" /> var arg1 = this.toString(); var r1, r2, m; try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 } try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 } m = Math.pow(10, Math.max(r1, r2)); return Number((arg1 * m + arg2 * m) / m); } Number.prototype.fSub = function (arg2) { /// <summary> /// 浮点减法运算(解决JS浮点运算的bug) /// </summary> /// <param name="arg2" type="int"> /// 右表达式 /// </param> /// <returns type="Number" /> var arg1 = this.toString(); var r1, r2, m, n; try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 } try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 } m = Math.pow(10, Math.max(r1, r2)); //动态控制精度长度 出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢! n = (r1 >= r2) ? r1 : r2; return Number(((arg1 * m - arg2 * m) / m).toFixed(n)); } Number.prototype.fMul = function (arg2) { /// <summary> /// 浮点乘法运算(解决JS浮点运算的bug) /// </summary> /// <param name="arg2" type="int"> /// 右表达式 /// </param> /// <returns type="Number" /> var arg1 = this; var m = 0, s1 = arg1.toString(), s2 = arg2.toString(); try { m += s1.split(".")[1].length } catch (e) { } try { m += s2.split(".")[1].length } catch (e) { } return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m); } Number.prototype.fDiv = function (arg2) { /// <summary> /// 浮点除法运算(解决JS浮点运算的bug) /// </summary> /// <param name="arg2" type="int"> /// 右表达式 /// </param> /// <returns type="Number" /> var arg1 = this; var t1 = 0, t2 = 0, r1, r2; try { t1 = arg1.toString().split(".")[1].length } catch (e) { } try { t2 = arg2.toString().split(".")[1].length } catch (e) { } with (Math) { r1 = Number(arg1.toString().replace(".", "")); r2 = Number(arg2.toString().replace(".", "")); return Number((r1 / r2) * pow(10, t2 - t1)); } } function strToJson(str) { /// <summary> /// 字符串转json /// </summary> /// <param name="str" type="String"> /// 要转换的字符串 出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢! /// </param> /// <returns type="Object" /> var json = (new Function("return " + str))(); return json; } String.prototype.toJson = function (formatStr) { /// <summary> /// 字符串转json /// </summary> /// <returns type="Object" /> return (new Function("return " + this))(); } Array.prototype.toJson = function () { var o = this; var r = []; if (typeof o == "string") return "\"" + o.replace(/([\'\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\""; if (typeof o == "object") { if (!o.sort) { for (var i in o) r.push(i + ":" + o[i].toJson()); if (!!document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)) { r.push("toString:" + o.toString.toString()); } r = "{" + r.join() + "}"; } else { for (var i = 0; i < o.length; i++) { r.push(o[i].toJson()); } r = "[" + r.join() + "]"; } return r; } return o.toString(); } function jsonDateFormat(jsonDate) { /// <summary> /// json日期格式转换为正常格式 /// </summary> /// <param name="jsonDate" type="String"> /// json日期 /// </param> /// <returns type="String" /> try {//出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢! var date = new Date(parseInt(jsonDate.replace("/Date(", "").replace(")/", ""), 10)); var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1; var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); var hours = date.getHours(); var minutes = date.getMinutes(); var seconds = date.getSeconds(); var milliseconds = date.getMilliseconds(); milliseconds = myPow(10, (3 - milliseconds.toString().length)).toString().substr(1) + milliseconds.toString(); return date.getFullYear() + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds + "." + milliseconds; } catch (ex) {//出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢! return ""; } /* 快速幂计算,传统计算方式如果幂次是100就要循环100遍求值 快速幂计算只需要循环7次即可 求x的y次方 x^y可以做如下分解 把y转换为2进制,设第n位的值为i,计算第n位的权为x^(2^(n-1)*i) 例如2^12 12的二进制是1100 12=2^3*1+2^2*1+2^1*0+2^0*0 因此2^12=2^(2^3+2^2) 分解得到2^12=2^(2^3)*2^(2^2) */ function myPow(dx, dy) { var r = 1; while (dy != 0) { var b = dy & 1; //取最末尾的一位数,也可以判断奇偶数,奇数:1,偶数:0 if (b) {//如果最末尾的数是1,储存有效值 r *= dx; } dx *= dx; //这里即完成了x^(2^(n-1)*i)的计算 dy >>= 1; //右位移去掉末尾1位,也可以看成是除以2取整数 } return r; } } String.prototype.toPriceString = function () { var arr = this.split('.'); var left = arr[0]; var right = "00"; if (arr.length > 1) { right = arr[1]; var rightL = right.length; if (rightL < 2) { right += "0"; } else if (rightL > 2) { right = right.substring(0, 2); } } var res = new Array(); while (left.length > 3) { res[res.length] = left.substr(-3); left = left.substring(0, left.length - 3); } res[res.length] = left; return res.reverse().join(','); }; Number.prototype.toPriceString = function () { return this.toString().toPriceString(); }; String.prototype.toUsuDate = function (formatStr) { /// <summary> /// json日期格式转换为正常格式 /// </summary> /// <param name="formatStr" type="String"> /// 格式化字符串格式 yyyy-mm-dd hh:mm:ss.fff 可以从尾部开始省略格式,不能截断或者从头开始省略 /// 可以是yy-m-d 或者yy-mm-d等等 出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢! /// </param> /// <returns type="String" /> if (formatStr == undefined || formatStr == null || formatStr == "") { return jsonDateFormat(this); } var formatArr = formatStr.split(/[-\s:\.]/g); var dateArr = jsonDateFormat(this).split(/[-\s:\.]/g); var resultData = []; if (formatArr.length > 7 || dateArr < 2) { return "0000-00-00"; } for (var i = 0, j = formatArr.length; i < j; i++) { switch (i) { case 0: var len = formatArr[i].length; dateArr[i] = dateArr[i].substr(-len); resultData[i] = dateArr[i]; break; case 1: if (dateArr[i] < 10) { var len = formatArr[i].length; dateArr[i] = dateArr[i].substr(-len); } resultData[i] = "-" + dateArr[i]; break; case 2: if (dateArr[i] < 10) { var len = formatArr[i].length; dateArr[i] = dateArr[i].substr(-len); } resultData[i] = "-" + dateArr[i]; break; case 3: if (dateArr[i] < 10) { var len = formatArr[i].length; dateArr[i] = dateArr[i].substr(-len); } resultData[i] = " " + dateArr[i]; break; case 4: if (dateArr[i] < 10) { var len = formatArr[i].length; dateArr[i] = dateArr[i].substr(-len); } resultData[i] = ":" + dateArr[i]; break; case 5: if (dateArr[i] < 10) { var len = formatArr[i].length; dateArr[i] = dateArr[i].substr(-len); } resultData[i] = ":" + dateArr[i]; break; case 6: var len = formatArr[i].length; resultData[i] = "." + dateArr[i].substr(-len); break; default: } } return resultData.join(''); } function GetRequest() { /// <summary> /// 获取url中"?"符后的字串 /// 使用方法: /// var Request = new Object(); /// Request = GetRequest(); /// var 参数1, 参数2, 参数3, 参数N; /// 参数1 = Request['参数1']; /// 参数2 = Request['参数2']; /// 参数3 = Request['参数3']; /// 参数N = Request['参数N']; /// </summary> /// <returns type="String" /> var url = location.search; var theRequest = new Object(); if (url.indexOf("?") != -1) { var str = url.substr(1); strs = str.split("&"); for (var i = 0; i < strs.length; i++) { theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]); } } return theRequest; } var Request = (function () { /// <summary> /// 获取url中"?"符后的字串 /// 使用方法: /// 参数1 = Request['参数1']; /// 参数2 = Request['参数2']; /// 参数3 = Request['参数3']; /// 参数N = Request['参数N']; /// </summary> /// <returns type="String" /> var url = location.search; var theRequest = new Object(); if (url.indexOf("?") != -1) { var str = url.substr(1); strs = str.split("&"); for (var i = 0; i < strs.length; i++) { theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]); } } return theRequest; })(); function isNullObj(obj) { /// <summary> /// 对象是否有值 /// </summary> /// <param name="obj" type="Object"> /// 对象obj 出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢! /// </param> /// <returns type="Boolean" /> for (var i in obj) { return false; } return true; } //Object.prototype.hasValue = function () {//Object的原型链增加方法会影响到jq和ztree // /// <summary> // /// 对象是否有值 // /// </summary> // /// <param name="obj" type="Object"> // /// 对象obj // /// </param> // /// <returns type="Boolean" /> // for (var i in this) { // return false; // } // return true; //} Serialize = function (obj) { /// <summary> /// 对象序列化 /// </summary> /// <param name="obj" type="Object"> /// 任意数据类型 /// </param> /// <returns type="String" /> function __typeof__(objClass) { if (objClass && objClass.constructor) { var strFun = objClass.constructor.toString(); var className = strFun.substr(0, strFun.indexOf('(')); className = className.replace('function', ''); return className.replace(/(^\s*)|(\s*$)/ig, ''); } return typeof (objClass); }; var type = __typeof__(obj); switch (type) { case 'Array': { var strArray = '['; for (var i = 0 ; i < obj.length ; ++i) { var value = ''; if (obj[i]) { value = Serialize(obj[i]); } strArray += value + ','; } if (strArray.charAt(strArray.length - 1) == ',') { strArray = strArray.substr(0, strArray.length - 1); } strArray += ']'; return strArray; } case 'Date': { return 'new Date(' + obj.getTime() + ')'; } case 'Function': { return obj; } case 'Number': case 'number': case 'Boolean': case 'String': { return "'" + obj.toString() + "'"; } default: { var serialize = '{'; for (var key in obj) { if (key == 'Serialize') continue; var subserialize = 'null'; if(obj.hasOwnProperty(key)){ if (obj[key] != undefined) { subserialize = Serialize(obj[key]); } serialize += '\r\n' + key + ' : ' + subserialize + ','; } } if (serialize.charAt(serialize.length - 1) == ',') { serialize = serialize.substr(0, serialize.length - 1); } serialize += '\r\n}'; return serialize; } } }; //sandbox (function () { var method; var noop = function () { }; var methods = [ 'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error', 'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log', 'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd', 'timeStamp', 'trace', 'warn' ]; var length = methods.length; var console = (window.console = window.console || {}); while (length--) { method = methods[length]; // Only stub undefined methods. if (!console[method]) { console[method] = noop; } } }()); var sandbox = new _sandbox(); /* sandbox.console.log("123"); //输出log sandbox.console.close(); //关闭沙箱控制台 sandbox.exec(function () { console.log("333"); });//执行一个代码块 sandbox.exec.close(); //关闭沙箱方法执行块 sandbox.close(); //关闭整个沙箱 */ function _sandbox() { /// <summary> /// 沙箱模式 出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢! /// </summary> window.sandboxConfig = { stop_exec: false }; }; _sandbox.prototype.close = function () { this.console.close(); this.exec.close(); }; _sandbox.prototype.console = (function () { return window.console; }()); _sandbox.prototype.console.close = function () { var noop = function () { }; var methods = [ 'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error', 'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log', 'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd', 'timeStamp', 'trace', 'warn' ]; var length = methods.length; var console = (window.console = window.console || {}); while (length--) { method = methods[length]; console[method] = noop; } }; _sandbox.prototype.exec = function (func) { if (!window.sandboxConfig.stop_exec) { if (typeof (func) == "function") { func(); } } //出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢! return this; }; _sandbox.prototype.exec.close = function () { window.sandboxConfig.stop_exec = true; }; function clone(jsonObj) { var buf; if (jsonObj instanceof Array) { buf = []; var i = jsonObj.length; while (i--) { buf[i] = arguments.callee(jsonObj[i]); } return buf; } else if (typeof jsonObj == "function") { return jsonObj; } else if (jsonObj instanceof Object) { buf = {}; for (var k in jsonObj) { buf[k] = arguments.callee(jsonObj[k]); } return buf; } else { return jsonObj; } }