var V = { }; var AP = ap = Array.prototype; var OP = op = Object.prototype; var objEqual = V.objEqual = function(objA, objB){ if (typeof arguments[0] != typeof arguments[1]) return false; //数组 if (arguments[0] instanceof Array){ if (arguments[0].length != arguments[1].length) return false; var allElementsEqual = true; for (var i = 0; i < arguments[0].length; ++i){ if (typeof arguments[0][i] != typeof arguments[1][i]){ return false; } if (typeof arguments[0][i] == 'number' && typeof arguments[1][i] == 'number'){ allElementsEqual = (arguments[0][i] == arguments[1][i]); } else{ allElementsEqual = arguments.callee(arguments[0][i], arguments[1][i]); //递归判断对象是否相等 } } return allElementsEqual; } //对象 if (arguments[0] instanceof Object && arguments[1] instanceof Object){ var result = true; var attributeLengthA = 0, attributeLengthB = 0; for (var o in arguments[0]){ //判断两个对象的同名属性是否相同(数字或字符串) if (typeof arguments[0][o] == 'number' || typeof arguments[0][o] == 'string'){ result = eval("arguments[0]['" + o + "'] == arguments[1]['" + o + "']"); } else { //如果对象的属性也是对象,则递归判断两个对象的同名属性 //if (!arguments.callee(arguments[0][o], arguments[1][o])) if (!arguments.callee(eval("arguments[0]['" + o + "']"), eval("arguments[1]['" + o + "']"))){ result = false; return result; } } ++attributeLengthA; } for (var o in arguments[1]) { ++attributeLengthB; } //如果两个对象的属性数目不等,则两个对象也不等 if (attributeLengthA != attributeLengthB){ result = false; } return result; } return arguments[0] == arguments[1]; }; var objIndexOf = V.objIndexOf = AP.objIndexOf = function(arr, obj){ if(arguments.length == 1){ if(!(this instanceof Array)){ return "参数不合法"; } for (var i = 0; i < this.length; i++) { if(objEqual(this[i],arguments[0])){ return i; } } }else{ if(!(arr instanceof Array)){ return "参数不合法"; } for (var i = 0; i < arr.length; i++) { if(objEqual(arr[i], obj)){ return i; } } } return -1 ; }; var hasContains = V.hasContains = AP.hasContains = function (arr, obj){ if(arguments.length == 1){ return (this.objIndexOf(arguments[0]) != -1) ; }else{ return (objIndexOf(arr, obj) != -1) ; } }; var removeObj = V.removeObj = AP.removeObj = function (arr, obj){ if(arguments.length == 1){ if(this.hasContains(arguments[0])){ var nindex = this.objIndexOf(arguments[0]); this.splice(nindex, 1); return this; } }else{ if(hasContains(arr, obj)){ var nindex = objIndexOf(arr, obj); arr.splice(nindex, 1); return arr; } } }; var removeAt = V.removeAt = AP.removeAt = function(arr,index){ if(arguments.length == 1){ this.splice(arguments[0],1); return this; }else{ arr.splice(index, 1); return arr; } }; var isArray = V.isArray = Array.isArray || function(object){ return object instanceof Array }; var isFunction = V.isFunction = function(value) { return type(value) == "function"; }; var type = V.type = function(obj) { var class2type = {}; return obj == null ? String(obj) : class2type[toString.call(obj)] || "object"; }; var isObject = function(obj){ return type(obj) == "object"; }; var objExtend = V.objExtend = function(target, source, deep){ for (var p in source) { if (source.hasOwnProperty(p)) { target[p] = source[p]; } } }; if(!AP.indexOf){ AP.indexOf = function(obj){ return objIndexOf(this, obj); } } var filter = V.filter = function(arr,callback){ var narr = []; for(var i = 0; i < arr.length; i++){ if (i in arr) { var val = arr[i]; // in case fun mutates this /* if (callback.call(arguments[1], val)){ narr.push(val); }*/ if (callback(val)){ narr.push(val); } } } return narr; }; AP.contains = function(obj,props){ /*var equals = true; for(var i = 0;i < this.length;i++){ var thisObj = this[i]; for(var j = 0;j < props.length;j++){ var prop = props[j]; equals = obj[prop] == thisObj[prop]?true:false; if(!equals){ break; } } if(equals){ break; } } return equals;*/ return (this.objIndexOf(arguments[0]) != -1) ; }; /** * each是一个集合迭代函数,它接受一个函数作为参数和一组可选的参数 * 这个迭代函数依次将集合的每一个元素和可选参数用函数进行计算,并将计算得的结果集返回 {%example <script> var a = [1,2,3,4].each(function(x){return x > 2 ? x : null}); var b = [1,2,3,4].each(function(x){return x < 0 ? x : null}); alert(a); alert(b); </script> %} * @param {Function} fn 进行迭代判定的函数 * @param more ... 零个或多个可选的用户自定义参数 * @returns {Array} 结果集,如果没有结果,返回空集 */ AP.each = function(fn){ fn = fn || Function.k; var narr = []; var args = AP.slice.call(arguments, 1); for(var i=0; i<this.length; i++){ var res = fn.apply(this,[this[i],i].concat(args)); if(res != null){ narr.push(res); } } return narr; }; /** * 得到一个数组不重复的元素集合 * 唯一化一个数组 * @returns {Array} 由不重复元素构成的数组 */ AP.unique = function(){ var ra = new Array(); for(var i = 0; i < this.length; i ++){ if(!ra.hasContains(this[i])){ ra.push(this[i]); } } return ra; }; /** * 求两个集合的补集 {%example <script> var a = [1,2,3,4]; var b = [3,4,5,6]; alert(Array.complement(a,b)); </script> %} * @param {Array} a 集合A * @param {Array} b 集合B * @returns {Array} 两个集合的补集 */ Array.complement = function(a, b){ return Array.minus(Array.union(a, b),Array.intersect(a, b)); }; /** * 求两个集合的交集 {%example <script> var a = [1,2,3,4]; var b = [3,4,5,6]; alert(Array.intersect(a,b)); </script> %} * @param {Array} a 集合A * @param {Array} b 集合B * @returns {Array} 两个集合的交集 */ Array.intersect = function(a, b){ return a.unique().each(function(o){return b.contains(o) ? o : null}); }; /** * 求两个集合的差集 {%example <script> var a = [1,2,3,4]; var b = [3,4,5,6]; alert(Array.minus(a,b)); </script> %} * @param {Array} a 集合A * @param {Array} b 集合B * @returns {Array} 两个集合的差集 */ Array.minus = function(a, b){ return a.unique().each(function(o){return b.contains(o) ? null : o}); }; /** * 求两个集合的并集 {%example <script> var a = [1,2,3,4]; var b = [3,4,5,6]; alert(Array.union(a,b)); </script> %} * @param {Array} a 集合A * @param {Array} b 集合B * @returns {Array} 两个集合的并集 */ Array.union = function(a, b){ return a.concat(b).unique(); }; // 比较两个 数组 是否 相等 Array.prototype.compare = function(testArr) { if (this.length != testArr.length) return false; for (var i = 0; i < testArr.length; i++) { if (this[i].compare) { if (!this[i].compare(testArr[i])) return false; } if (this[i] !== testArr[i]) return false; } return true; }; // 数组插入 Array.prototype.insertAt = function($value, $index) { if($index < 0) this.unshift($value); else if($index >= this.length) this.push($value); else this.splice($index, 0, $value); }; /** * 根据数组的下标来删除元素 */ Array.prototype.removeByIndex=function($n) { if($n<0){ //如果n<0,则不进行任何操作。 return this; }else{ return this.slice(0,$n).concat(this.slice($n+1,this.length)); } } //依赖indexOf Array.prototype.remove = function($value) { var $index = this.indexOf($value); if($index != -1) this.splice($index, 1); } Array.prototype.removeAll = function() { while(this.length > 0) this.pop(); } Array.prototype.replace = function($oldValue, $newValue) { for(var $i=0; $i<this.length; $i++) { if(this[$i] == $oldValue) { this[$i] = $newValue; return; } } } Array.prototype.swap = function($a, $b) { if($a == $b) return; var $tmp = this[$a]; this[$a] = this[$b]; this[$b] = $tmp; } Array.prototype.max = function() { return Math.max.apply({}, this); } Array.prototype.min = function() { return Math.min.apply({}, this); } // JS创建新对象,扩展对象和拷贝对象方法的实现 /*(function(exports){ var mObject=(function(){ return { //以参数作为原型,创建一个新的对象 fCreate:function(oProto){ var F=function(){}; F.prototype=oProto; return new F(); }, //扩展一个原有对象 fExtend:function(o,oAdded){ for(var i in oAdded) { o[i]=oAdded[i]; } }, //深拷贝一个对象 fCopy:function(o){ var oRes; if(o instanceof Array) { oRes=[]; } else if(o instanceof Object){ oRes={}; } else{ return o; } for(var i in o) { if(typeof o[i] !='object') { oRes[i]=o[i]; } else{ oRes[i]=arguments.callee(o[i]); } } return oRes; } }; })(); exports.mObject=mObject; })(window);*/ if(typeof String.prototype.ltrim=='undefined'){ String.prototype.ltrim = function(){ var s = this; s = s.replace(/^\s*/g, ''); return s; } } if(typeof String.prototype.rtrim=='undefined'){ String.prototype.rtrim = function(){ var s = this; s = s.replace(/\s*$/g, ''); return s; } } if(typeof String.prototype.trim=='undefined'){ String.prototype.trim = function(){ return this.ltrim().rtrim(); } } if(typeof String.prototype.htmlEncode=='undefined'){ String.prototype.htmlEncode = function(encodeNewLine){//encodeNewLine:是否encode换行符 var s = this; s = s.replace(/&/g, '&'); s = s.replace(/</g, '<'); s = s.replace(/>/g, '>'); s = s.replace(/'/g, '"'); if(encodeNewLine){ s = s.replace(/\r\n/g, '<br />'); s = s.replace(/\r/g, '<br />'); s = s.replace(/\n/g, '<br />'); } return s; } } function cloneObj(oldObj) { //复制对象方法 if (typeof(oldObj) != 'object') return oldObj; if (oldObj == null) return oldObj; var newObj = Object(); for (var i in oldObj) newObj[i] = cloneObj(oldObj[i]); return newObj; } function extendObj() { //扩展对象 var args = arguments;//将传递过来的参数数组赋值给args变量 if (args.length < 2) return; var temp = cloneObj(args[0]); //调用复制对象方法 for (var n = 1; n < args.length; n++) { for (var i in args[n]) { temp[i] = args[n][i]; } } return temp; } // es5-safe 模块 /*Function.prototype.bind Object.create Object.keys Array.isArray Array.prototype.forEach Array.prototype.map Array.prototype.filter Array.prototype.every Array.prototype.some Array.prototype.reduce Array.prototype.reduceRight Array.prototype.indexOf Array.prototype.lastIndexOf String.prototype.trim Date.now*/ if (!Array.prototype.every){ Array.prototype.every = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "function") throw new TypeError(); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this && !fun.call(thisp, this[i], i, this)) return false; } return true; }; } if (!Array.prototype.filter) { Array.prototype.filter = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "function") throw new TypeError(); var res = new Array(); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this) { var val = this[i]; // in case fun mutates this if (fun.call(thisp, val, i, this)) res.push(val); } } return res; }; } if (!Array.prototype.forEach) { Array.prototype.forEach = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "function") throw new TypeError(); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this) fun.call(thisp, this[i], i, this); } }; } if (!Array.prototype.map) { Array.prototype.map = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "function") throw new TypeError(); var res = new Array(len); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this) res[i] = fun.call(thisp, this[i], i, this); } return res; }; } if (!Array.prototype.some) { Array.prototype.some = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "function") throw new TypeError(); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this && fun.call(thisp, this[i], i, this)) return true; } return false; }; } Array.prototype.sortNum = function() { return this.sort( function (a,b) { return a-b; } ); } if(typeof Object.create !== "function"){ Object.create = function (o) { function F(){}; F.property = o; return new F(); } } var Model = { inherited: function(){}, created: function(){}, prototype: { init: function(){} }, create: function(){ var object = Object.create(this); object.parent = this; object.prototype = object.fn = Object.create(this.prototype); object.created(); this.inherited(object); return object; }, init: function(){ var instance = Object.create(this.prototype); instance.parent = this; instance.init.apply(instance, arguments); return instance; } }; objExtend(Model, { find: function(){} }); objExtend(Model.prototype,{ init: function(atts){ if(atts){ this.load(atts); } }, load: function(attr){ for(var name in attr) this[name] = attr[name]; } }); var modelStore = V.modelStore = function(){ } var assert = function(value, msg){ if(!value){ throw(msg || (value + " does not equal true")); } }; var assertEqual = function(val1, val2, msg) { if (val1 !== val2){ throw(msg || (val1 + " does not equal " + val2)); } }; // 清除两边的空格 String.prototype.trim = function() { return this.replace(/(^\s*)|(\s*$)/g, ''); }; // 合并多个空白为一个空白 String.prototype.ResetBlank = function() { var regEx = /\s+/g; return this.replace(regEx, ' '); }; // 保留数字 String.prototype.GetNum = function() { var regEx = /[^\d]/g; return this.replace(regEx, ''); }; // 保留中文 String.prototype.GetCN = function() { var regEx = /[^\u4e00-\u9fa5\uf900-\ufa2d]/g; return this.replace(regEx, ''); }; // String转化为Number String.prototype.ToInt = function() { return isNaN(parseInt(this)) ? this.toString() : parseInt(this); }; // 得到字节长度 String.prototype.GetLen = function() { var regEx = /^[\u4e00-\u9fa5\uf900-\ufa2d]+$/; if (regEx.test(this)) { return this.length * 2; } else { var oMatches = this.match(/[\x00-\xff]/g); var oLength = this.length * 2 - oMatches.length; return oLength; } }; // 获取文件全名 String.prototype.GetFileName = function() { var regEx = /^.*\/([^\/\?]*).*$/; return this.replace(regEx, '$1'); }; // 获取文件扩展名 String.prototype.GetExtensionName = function() { var regEx = /^.*\/[^\/]*(\.[^\.\?]*).*$/; return this.replace(regEx, '$1'); }; //替换所有 String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) { if (!RegExp.prototype.isPrototypeOf(reallyDo)) { return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi" : "g")), replaceWith); } else { return this.replace(reallyDo, replaceWith); } }; //格式化字符串 add By 刘景宁 2010-12-09 String.Format = function() { if (arguments.length == 0) { return ''; } if (arguments.length == 1) { return arguments[0]; } var reg = /{(\d+)?}/g; var args = arguments; var result = arguments[0].replace(reg, function($0, $1) { return args[parseInt($1) + 1]; }); return result; }; // 数字补零 Number.prototype.LenWithZero = function(oCount) { var strText = this.toString(); while (strText.length < oCount) { strText = '0' + strText; } return strText; }; // Unicode还原 Number.prototype.ChrW = function() { return String.fromCharCode(this); }; // 数字数组由小到大排序 Array.prototype.Min2Max = function() { var oValue; for (var i = 0; i < this.length; i++) { for (var j = 0; j <= i; j++) { if (this[i] < this[j]) { oValue = this[i]; this[i] = this[j]; this[j] = oValue; } } } return this; }; // 数字数组由大到小排序 Array.prototype.Max2Min = function() { var oValue; for (var i = 0; i < this.length; i++) { for (var j = 0; j <= i; j++) { if (this[i] > this[j]) { oValue = this[i]; this[i] = this[j]; this[j] = oValue; } } } return this; }; // 获得数字数组中最大项 Array.prototype.GetMax = function() { var oValue = 0; for (var i = 0; i < this.length; i++) { if (this[i] > oValue) { oValue = this[i]; } } return oValue; }; // 获得数字数组中最小项 Array.prototype.GetMin = function() { var oValue = 0; for (var i = 0; i < this.length; i++) { if (this[i] < oValue) { oValue = this[i]; } } return oValue; }; // 获取当前时间的中文形式 Date.prototype.GetCNDate = function() { var oDateText = ''; oDateText += this.getFullYear().LenWithZero(4) + new Number(24180).ChrW(); oDateText += this.getMonth().LenWithZero(2) + new Number(26376).ChrW(); oDateText += this.getDate().LenWithZero(2) + new Number(26085).ChrW(); oDateText += this.getHours().LenWithZero(2) + new Number(26102).ChrW(); oDateText += this.getMinutes().LenWithZero(2) + new Number(20998).ChrW(); oDateText += this.getSeconds().LenWithZero(2) + new Number(31186).ChrW(); oDateText += new Number(32).ChrW() + new Number(32).ChrW() + new Number(26143).ChrW() + new Number(26399).ChrW() + new String('26085199682010819977222352011620845').substr(this.getDay() * 5, 5).ToInt().ChrW(); return oDateText; }; //扩展Date格式化 Date.prototype.Format = function(format) { var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时 "H+": this.getHours(), //小时 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; var week = { "0": "\u65e5", "1": "\u4e00", "2": "\u4e8c", "3": "\u4e09", "4": "\u56db", "5": "\u4e94", "6": "\u516d" }; if (/(y+)/.test(format)) { format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); } if (/(E+)/.test(format)) { format = format.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "\u661f\u671f" : "\u5468") : "") + week[this.getDay() + ""]); } 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; } Date.prototype.Diff = function(interval, objDate) { //若参数不足或 objDate 不是日期类型則回传 undefined if (arguments.length < 2 || objDate.constructor != Date) { return undefined; } switch (interval) { //计算秒差 case 's': return parseInt((objDate - this) / 1000); //计算分差 case 'n': return parseInt((objDate - this) / 60000); //计算時差 case 'h': return parseInt((objDate - this) / 3600000); //计算日差 case 'd': return parseInt((objDate - this) / 86400000); //计算周差 case 'w': return parseInt((objDate - this) / (86400000 * 7)); //计算月差 case 'm': return (objDate.getMonth() + 1) + ((objDate.getFullYear() - this.getFullYear()) * 12) - (this.getMonth() + 1); //计算年差 case 'y': return objDate.getFullYear() - this.getFullYear(); //输入有误 default: return undefined; } }; //检测是否为空 /* Object.prototype.IsNullOrEmpty = function() { /!*var obj = this; var flag = false; if (obj == null || obj == undefined || typeof (obj) == 'undefined' || obj == '') { flag = true; } else if (typeof (obj) == 'string') { obj = obj.trim(); if (obj == '') {//为空 flag = true; } else {//不为空 obj = obj.toUpperCase(); if (obj == 'NULL' || obj == 'UNDEFINED' || obj == '{}') { flag = true; } } } else { flag = false; } return flag;*!/ };*/ // 事件绑定 var delegate = V.delegate = function(parent, tagName, evtype, callback){ parent['on'+evtype] = function(ev){ var ev = ev || window.event; var tar = ev.target || ev.srcElement; if(tar.tagName.toLowerCase() === tagName){ callback && callback(tar); } } };
var V = { };var AP = ap = Array.prototype;var OP = op = Object.prototype;var objEqual = V.objEqual = function(objA, objB){
if (typeof arguments[0] != typeof arguments[1]) return false;
//数组 if (arguments[0] instanceof Array){ if (arguments[0].length != arguments[1].length) return false; var allElementsEqual = true; for (var i = 0; i < arguments[0].length; ++i){ if (typeof arguments[0][i] != typeof arguments[1][i]){ return false; }
if (typeof arguments[0][i] == 'number' && typeof arguments[1][i] == 'number'){ allElementsEqual = (arguments[0][i] == arguments[1][i]); } else{ allElementsEqual = arguments.callee(arguments[0][i], arguments[1][i]); //递归判断对象是否相等 } } return allElementsEqual; } //对象 if (arguments[0] instanceof Object && arguments[1] instanceof Object){ var result = true; var attributeLengthA = 0, attributeLengthB = 0; for (var o in arguments[0]){ //判断两个对象的同名属性是否相同(数字或字符串) if (typeof arguments[0][o] == 'number' || typeof arguments[0][o] == 'string'){ result = eval("arguments[0]['" + o + "'] == arguments[1]['" + o + "']"); } else { //如果对象的属性也是对象,则递归判断两个对象的同名属性 //if (!arguments.callee(arguments[0][o], arguments[1][o])) if (!arguments.callee(eval("arguments[0]['" + o + "']"), eval("arguments[1]['" + o + "']"))){ result = false; return result; } } ++attributeLengthA; } for (var o in arguments[1]) { ++attributeLengthB; } //如果两个对象的属性数目不等,则两个对象也不等 if (attributeLengthA != attributeLengthB){ result = false; } return result; } return arguments[0] == arguments[1];};
var objIndexOf = V.objIndexOf = AP.objIndexOf = function(arr, obj){ if(arguments.length == 1){ if(!(this instanceof Array)){ return "参数不合法"; } for (var i = 0; i < this.length; i++) { if(objEqual(this[i],arguments[0])){ return i; } } }else{ if(!(arr instanceof Array)){ return "参数不合法"; } for (var i = 0; i < arr.length; i++) { if(objEqual(arr[i], obj)){ return i; } } } return -1 ;};
var hasContains = V.hasContains = AP.hasContains = function (arr, obj){ if(arguments.length == 1){ return (this.objIndexOf(arguments[0]) != -1) ; }else{ return (objIndexOf(arr, obj) != -1) ; }};
var removeObj = V.removeObj = AP.removeObj = function (arr, obj){ if(arguments.length == 1){ if(this.hasContains(arguments[0])){ var nindex = this.objIndexOf(arguments[0]); this.splice(nindex, 1); return this; } }else{ if(hasContains(arr, obj)){ var nindex = objIndexOf(arr, obj); arr.splice(nindex, 1); return arr; } }};
var removeAt = V.removeAt = AP.removeAt = function(arr,index){ if(arguments.length == 1){ this.splice(arguments[0],1); return this; }else{ arr.splice(index, 1); return arr; }};
var isArray = V.isArray = Array.isArray || function(object){ return object instanceof Array };var isFunction = V.isFunction = function(value) { return type(value) == "function";};
var type = V.type = function(obj) { var class2type = {}; return obj == null ? String(obj) : class2type[toString.call(obj)] || "object";};
var isObject = function(obj){ return type(obj) == "object";};
var objExtend = V.objExtend = function(target, source, deep){ for (var p in source) { if (source.hasOwnProperty(p)) { target[p] = source[p]; } }};
if(!AP.indexOf){ AP.indexOf = function(obj){ return objIndexOf(this, obj); }}
var filter = V.filter = function(arr,callback){ var narr = [];
for(var i = 0; i < arr.length; i++){ if (i in arr) { var val = arr[i]; // in case fun mutates this /* if (callback.call(arguments[1], val)){ narr.push(val); }*/ if (callback(val)){ narr.push(val); } } } return narr;};
AP.contains = function(obj,props){ /*var equals = true; for(var i = 0;i < this.length;i++){ var thisObj = this[i];
for(var j = 0;j < props.length;j++){ var prop = props[j]; equals = obj[prop] == thisObj[prop]?true:false; if(!equals){ break; } } if(equals){ break; } } return equals;*/ return (this.objIndexOf(arguments[0]) != -1) ;};
/** * each是一个集合迭代函数,它接受一个函数作为参数和一组可选的参数 * 这个迭代函数依次将集合的每一个元素和可选参数用函数进行计算,并将计算得的结果集返回 {%example <script> var a = [1,2,3,4].each(function(x){return x > 2 ? x : null}); var b = [1,2,3,4].each(function(x){return x < 0 ? x : null}); alert(a); alert(b); </script> %} * @param {Function} fn 进行迭代判定的函数 * @param more ... 零个或多个可选的用户自定义参数 * @returns {Array} 结果集,如果没有结果,返回空集 */AP.each = function(fn){ fn = fn || Function.k; var narr = []; var args = AP.slice.call(arguments, 1); for(var i=0; i<this.length; i++){ var res = fn.apply(this,[this[i],i].concat(args)); if(res != null){ narr.push(res); } } return narr;};
/** * 得到一个数组不重复的元素集合
* 唯一化一个数组 * @returns {Array} 由不重复元素构成的数组 */AP.unique = function(){ var ra = new Array(); for(var i = 0; i < this.length; i ++){ if(!ra.hasContains(this[i])){ ra.push(this[i]); } } return ra;};
/** * 求两个集合的补集 {%example <script> var a = [1,2,3,4]; var b = [3,4,5,6]; alert(Array.complement(a,b)); </script> %} * @param {Array} a 集合A * @param {Array} b 集合B * @returns {Array} 两个集合的补集 */Array.complement = function(a, b){ return Array.minus(Array.union(a, b),Array.intersect(a, b));};
/** * 求两个集合的交集 {%example <script> var a = [1,2,3,4]; var b = [3,4,5,6]; alert(Array.intersect(a,b)); </script> %} * @param {Array} a 集合A * @param {Array} b 集合B * @returns {Array} 两个集合的交集 */Array.intersect = function(a, b){ return a.unique().each(function(o){return b.contains(o) ? o : null});};
/** * 求两个集合的差集 {%example <script> var a = [1,2,3,4]; var b = [3,4,5,6]; alert(Array.minus(a,b)); </script> %} * @param {Array} a 集合A * @param {Array} b 集合B * @returns {Array} 两个集合的差集 */Array.minus = function(a, b){ return a.unique().each(function(o){return b.contains(o) ? null : o});};
/** * 求两个集合的并集 {%example <script> var a = [1,2,3,4]; var b = [3,4,5,6]; alert(Array.union(a,b)); </script> %} * @param {Array} a 集合A * @param {Array} b 集合B * @returns {Array} 两个集合的并集 */Array.union = function(a, b){ return a.concat(b).unique();};
// 比较两个 数组 是否 相等Array.prototype.compare = function(testArr) { if (this.length != testArr.length) return false; for (var i = 0; i < testArr.length; i++) { if (this[i].compare) { if (!this[i].compare(testArr[i])) return false; } if (this[i] !== testArr[i]) return false; } return true;};
// 数组插入Array.prototype.insertAt = function($value, $index) { if($index < 0) this.unshift($value); else if($index >= this.length) this.push($value); else this.splice($index, 0, $value);};
/** * 根据数组的下标来删除元素 */Array.prototype.removeByIndex=function($n) { if($n<0){ //如果n<0,则不进行任何操作。 return this; }else{ return this.slice(0,$n).concat(this.slice($n+1,this.length)); }}//依赖indexOfArray.prototype.remove = function($value){ var $index = this.indexOf($value);
if($index != -1) this.splice($index, 1);}
Array.prototype.removeAll = function(){ while(this.length > 0) this.pop();}
Array.prototype.replace = function($oldValue, $newValue){ for(var $i=0; $i<this.length; $i++) { if(this[$i] == $oldValue) { this[$i] = $newValue; return; } }}
Array.prototype.swap = function($a, $b){ if($a == $b) return;
var $tmp = this[$a]; this[$a] = this[$b]; this[$b] = $tmp;}Array.prototype.max = function() { return Math.max.apply({}, this);}Array.prototype.min = function() { return Math.min.apply({}, this);}
// JS创建新对象,扩展对象和拷贝对象方法的实现/*(function(exports){ var mObject=(function(){ return { //以参数作为原型,创建一个新的对象 fCreate:function(oProto){ var F=function(){}; F.prototype=oProto; return new F(); }, //扩展一个原有对象 fExtend:function(o,oAdded){ for(var i in oAdded) { o[i]=oAdded[i]; } }, //深拷贝一个对象 fCopy:function(o){ var oRes; if(o instanceof Array) { oRes=[]; } else if(o instanceof Object){ oRes={}; } else{ return o; } for(var i in o) { if(typeof o[i] !='object') { oRes[i]=o[i]; } else{ oRes[i]=arguments.callee(o[i]); } } return oRes; } }; })(); exports.mObject=mObject;})(window);*/
if(typeof String.prototype.ltrim=='undefined'){ String.prototype.ltrim = function(){ var s = this; s = s.replace(/^\s*/g, ''); return s; }}
if(typeof String.prototype.rtrim=='undefined'){ String.prototype.rtrim = function(){ var s = this; s = s.replace(/\s*$/g, ''); return s; }}
if(typeof String.prototype.trim=='undefined'){ String.prototype.trim = function(){ return this.ltrim().rtrim(); }}
if(typeof String.prototype.htmlEncode=='undefined'){ String.prototype.htmlEncode = function(encodeNewLine){//encodeNewLine:是否encode换行符 var s = this; s = s.replace(/&/g, '&'); s = s.replace(/</g, '<'); s = s.replace(/>/g, '>'); s = s.replace(/'/g, '"'); if(encodeNewLine){ s = s.replace(/\r\n/g, '<br />'); s = s.replace(/\r/g, '<br />'); s = s.replace(/\n/g, '<br />'); } return s; }}
function cloneObj(oldObj) { //复制对象方法 if (typeof(oldObj) != 'object') return oldObj; if (oldObj == null) return oldObj; var newObj = Object(); for (var i in oldObj) newObj[i] = cloneObj(oldObj[i]); return newObj;}
function extendObj() { //扩展对象 var args = arguments;//将传递过来的参数数组赋值给args变量 if (args.length < 2) return; var temp = cloneObj(args[0]); //调用复制对象方法 for (var n = 1; n < args.length; n++) { for (var i in args[n]) { temp[i] = args[n][i]; } } return temp;}
// es5-safe 模块/*Function.prototype.bindObject.createObject.keysArray.isArrayArray.prototype.forEachArray.prototype.mapArray.prototype.filterArray.prototype.everyArray.prototype.someArray.prototype.reduceArray.prototype.reduceRightArray.prototype.indexOfArray.prototype.lastIndexOfString.prototype.trimDate.now*/
if (!Array.prototype.every){ Array.prototype.every = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "function") throw new TypeError();
var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this && !fun.call(thisp, this[i], i, this)) return false; }
return true; };}if (!Array.prototype.filter) { Array.prototype.filter = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "function") throw new TypeError();
var res = new Array(); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this) { var val = this[i]; // in case fun mutates this if (fun.call(thisp, val, i, this)) res.push(val); } }
return res; };}if (!Array.prototype.forEach){ Array.prototype.forEach = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "function") throw new TypeError();
var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this) fun.call(thisp, this[i], i, this); } };}if (!Array.prototype.map){ Array.prototype.map = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "function") throw new TypeError();
var res = new Array(len); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this) res[i] = fun.call(thisp, this[i], i, this); }
return res; };}if (!Array.prototype.some){ Array.prototype.some = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "function") throw new TypeError();
var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in this && fun.call(thisp, this[i], i, this)) return true; }
return false; };}Array.prototype.sortNum = function() { return this.sort( function (a,b) { return a-b; } );}
if(typeof Object.create !== "function"){ Object.create = function (o) { function F(){}; F.property = o; return new F(); }}
var Model = { inherited: function(){}, created: function(){}, prototype: { init: function(){} }, create: function(){ var object = Object.create(this); object.parent = this; object.prototype = object.fn = Object.create(this.prototype); object.created(); this.inherited(object); return object; }, init: function(){ var instance = Object.create(this.prototype); instance.parent = this; instance.init.apply(instance, arguments); return instance; }};
objExtend(Model, { find: function(){}});
objExtend(Model.prototype,{ init: function(atts){ if(atts){ this.load(atts); } }, load: function(attr){ for(var name in attr) this[name] = attr[name]; }});
var modelStore = V.modelStore = function(){
}
var assert = function(value, msg){if(!value){throw(msg || (value + " does not equal true"));}};
var assertEqual = function(val1, val2, msg) {if (val1 !== val2){throw(msg || (val1 + " does not equal " + val2));}};
// 清除两边的空格String.prototype.trim = function() { return this.replace(/(^\s*)|(\s*$)/g, '');};// 合并多个空白为一个空白String.prototype.ResetBlank = function() { var regEx = /\s+/g; return this.replace(regEx, ' ');};
// 保留数字String.prototype.GetNum = function() { var regEx = /[^\d]/g; return this.replace(regEx, '');};
// 保留中文String.prototype.GetCN = function() { var regEx = /[^\u4e00-\u9fa5\uf900-\ufa2d]/g; return this.replace(regEx, '');};
// String转化为NumberString.prototype.ToInt = function() { return isNaN(parseInt(this)) ? this.toString() : parseInt(this);};
// 得到字节长度String.prototype.GetLen = function() { var regEx = /^[\u4e00-\u9fa5\uf900-\ufa2d]+$/; if (regEx.test(this)) { return this.length * 2; } else { var oMatches = this.match(/[\x00-\xff]/g); var oLength = this.length * 2 - oMatches.length; return oLength; }};
// 获取文件全名String.prototype.GetFileName = function() { var regEx = /^.*\/([^\/\?]*).*$/; return this.replace(regEx, '$1');};
// 获取文件扩展名String.prototype.GetExtensionName = function() { var regEx = /^.*\/[^\/]*(\.[^\.\?]*).*$/; return this.replace(regEx, '$1');};
//替换所有String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) { if (!RegExp.prototype.isPrototypeOf(reallyDo)) { return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi" : "g")), replaceWith); } else { return this.replace(reallyDo, replaceWith); }};//格式化字符串 add By 刘景宁 2010-12-09String.Format = function() { if (arguments.length == 0) { return ''; }
if (arguments.length == 1) { return arguments[0]; }
var reg = /{(\d+)?}/g; var args = arguments; var result = arguments[0].replace(reg, function($0, $1) { return args[parseInt($1) + 1]; }); return result;};
// 数字补零Number.prototype.LenWithZero = function(oCount) { var strText = this.toString(); while (strText.length < oCount) { strText = '0' + strText; } return strText;};
// Unicode还原Number.prototype.ChrW = function() { return String.fromCharCode(this);};
// 数字数组由小到大排序Array.prototype.Min2Max = function() { var oValue; for (var i = 0; i < this.length; i++) { for (var j = 0; j <= i; j++) { if (this[i] < this[j]) { oValue = this[i]; this[i] = this[j]; this[j] = oValue; } } } return this;};
// 数字数组由大到小排序Array.prototype.Max2Min = function() { var oValue; for (var i = 0; i < this.length; i++) { for (var j = 0; j <= i; j++) { if (this[i] > this[j]) { oValue = this[i]; this[i] = this[j]; this[j] = oValue; } } } return this;};
// 获得数字数组中最大项Array.prototype.GetMax = function() { var oValue = 0; for (var i = 0; i < this.length; i++) { if (this[i] > oValue) { oValue = this[i]; } } return oValue;};
// 获得数字数组中最小项Array.prototype.GetMin = function() { var oValue = 0; for (var i = 0; i < this.length; i++) { if (this[i] < oValue) { oValue = this[i]; } } return oValue;};
// 获取当前时间的中文形式Date.prototype.GetCNDate = function() { var oDateText = ''; oDateText += this.getFullYear().LenWithZero(4) + new Number(24180).ChrW(); oDateText += this.getMonth().LenWithZero(2) + new Number(26376).ChrW(); oDateText += this.getDate().LenWithZero(2) + new Number(26085).ChrW(); oDateText += this.getHours().LenWithZero(2) + new Number(26102).ChrW(); oDateText += this.getMinutes().LenWithZero(2) + new Number(20998).ChrW(); oDateText += this.getSeconds().LenWithZero(2) + new Number(31186).ChrW(); oDateText += new Number(32).ChrW() + new Number(32).ChrW() + new Number(26143).ChrW() + new Number(26399).ChrW() + new String('26085199682010819977222352011620845').substr(this.getDay() * 5, 5).ToInt().ChrW(); return oDateText;};//扩展Date格式化Date.prototype.Format = function(format) { var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时 "H+": this.getHours(), //小时 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; var week = { "0": "\u65e5", "1": "\u4e00", "2": "\u4e8c", "3": "\u4e09", "4": "\u56db", "5": "\u4e94", "6": "\u516d" }; if (/(y+)/.test(format)) { format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); } if (/(E+)/.test(format)) { format = format.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "\u661f\u671f" : "\u5468") : "") + week[this.getDay() + ""]); } 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;}Date.prototype.Diff = function(interval, objDate) { //若参数不足或 objDate 不是日期类型則回传 undefined if (arguments.length < 2 || objDate.constructor != Date) { return undefined; } switch (interval) { //计算秒差 case 's': return parseInt((objDate - this) / 1000); //计算分差 case 'n': return parseInt((objDate - this) / 60000); //计算時差 case 'h': return parseInt((objDate - this) / 3600000); //计算日差 case 'd': return parseInt((objDate - this) / 86400000); //计算周差 case 'w': return parseInt((objDate - this) / (86400000 * 7)); //计算月差 case 'm': return (objDate.getMonth() + 1) + ((objDate.getFullYear() - this.getFullYear()) * 12) - (this.getMonth() + 1); //计算年差 case 'y': return objDate.getFullYear() - this.getFullYear(); //输入有误 default: return undefined; }};
//检测是否为空/*Object.prototype.IsNullOrEmpty = function() { /!*var obj = this; var flag = false; if (obj == null || obj == undefined || typeof (obj) == 'undefined' || obj == '') { flag = true; } else if (typeof (obj) == 'string') { obj = obj.trim(); if (obj == '') {//为空 flag = true; } else {//不为空 obj = obj.toUpperCase(); if (obj == 'NULL' || obj == 'UNDEFINED' || obj == '{}') { flag = true; } } } else { flag = false; } return flag;*!/};*/
// 事件绑定var delegate = V.delegate = function(parent, tagName, evtype, callback){ parent['on'+evtype] = function(ev){ var ev = ev || window.event; var tar = ev.target || ev.srcElement; if(tar.tagName.toLowerCase() === tagName){ callback && callback(tar); } }};
来源:https://www.cnblogs.com/vsmart/p/7597287.html