国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

js實現prototype擴展的方法(字符串,日期,數組擴展)

2019-11-20 10:47:59
字體:
來源:轉載
供稿:網友

本文實例講述了js實現prototype擴展的方法。分享給大家供大家參考,具體如下:

String.prototype.isEmpty = function () { return !(/.?[^/s ]+/.test(this)); } //檢測字符串是否為空// 替換字符String.prototype.reserve = function(type) { if (type == 'int') return this.replace(/^/d/g, ''); // 替換字符串中除了數字以外的所有字符 else if (type == 'en') return this.replace(/[^A-Za-z]/g, ''); // 替換字符串中除了英文以外的所有字符 else if (type == 'cn') return this.replace(/[^/u4e00-/u9fa5/uf900-/ufa2d]/g, ''); // 替換字符串中除了中文以外的所有字符 else return this;}// 字符串反轉String.prototype.reverse = function() { return this.split('').reverse().join('');}// 以一個中文算兩個字符長度計算字符串的長度String.prototype.cnLength = function() { return this.replace(/[^/x00-/xff]/g, ' * * ' ).length; }// 替換字符串中的空格String.prototype.trim = function(type, char) { var type = type ? type.toUpperCase() : ''; switch (type) {  case 'B' : // 替換所有欲清除字符,未定義char則默認為替換空格   return this.replace(char ? new RegExp(char, 'g') : /(/s+| )/g, '');  case 'O' : // 將兩個以上的連續欲清除字符替換為一個,未定義char則默認為替換空格   return char ? this.replace(new RegExp(char + '{2,}', 'g'), char) : this.replace(/[/s ]{2,}/g, ' ');  case 'L' : // 替換除左邊欲清除字符,未定義char則默認為替換空格   return this.replace(char ? new RegExp('^(' + char + ') * ', 'g') : /^(/s| ) * /g, '');  case 'R' : // 替換除右邊欲清除字符,未定義char則默認為替換空格   return this.replace(char ? new RegExp('(' + char + ') * $', 'g') : /(/s| ) * $/g, '');  default : // 替換除左右兩邊欲清除字符,未定義char則默認為替換空格   return this.replace(char ? new RegExp('^(' + char + ') * |(' + char + ') * $', 'g') : /(^/s * | )|( |/s * $)/g, ''); }}// 判斷字符串是否是數字String.prototype.isNumer = function(flag) { if (isNaN(this)) {return false;} switch (flag) {  case '+' : return /(^/+?|^/d?)/d * /.?/d+$/.test(this); // 正數  case '-' : return /^-/d * /.?/d+$/.test(this); // 負數  case 'i' : return /(^-?|^/+?|/d)/d+$/.test(this); // 整數  case '+i' : return /(^/d+$)|(^/+?/d+$)/.test(this); // 正整數  case '-i' : return /^-/d+$/.test(this); // 負整數  case 'f' : return /(^-?|^/+?|^/d?)/d * /./d+$/.test(this); // 浮點數  case '+f' : return /(^/+?|^/d?)/d * /./d+$/.test(this); // 正浮點數  case '-f' : return /^-/d * /./d$/.test(this); // 負浮點數  default : return true; // 缺省 }}// 仿PHP的str_padString.prototype.pad = function (input, length, type) { if (!input) return this; if (!length || length < 1) var length = 1; var input = Array(length + 1).join(input), value; var type = type ? type.toUpperCase() : ''; switch (type) {  case 'LEFT' : return input + this;  case 'BOTH' : return input + this + input;  default : return this + input; }}// 獲取url對應參數的值String.prototype.getQuery = function(name) { var reg = new RegExp('(^|&)' + name + ' = ([^&] * )(&|$)'); var r = this.substr(this.indexOf('/?') + 1).match(reg); return r[2]?unescape(r[2]) : null;}// 判斷是否是日期格式(YYYY-MM-DD YYYY/MM/DD YYYY.MM.DD)String.prototype.isDate = function() { result = this.match(/^(/d{1, 4})(-|//|.)(/d{1, 2})/2(/d{1, 2})$/); if (!result) return false; var d = new Date(result[1], result[3]-1, result[4]) var str = d.getFullYear() + result[2] + (d.getMonth() + 1) + result[2] + d.getDate(); return this == str;}// 將字符串轉為日期String.prototype.toDate = function() { var mDate = new Date(Date.parse(str)); if (isNaN(mDate)) {  var arr = this.split('-');  mDate = new Date(arr[0], arr[1], arr[2]); } return mDate;}// 格式化日期, new Date().format('yyyy/mm/dd')Date.prototype.format = function(format) { var format = format.toLowerCase(); var type = {  'm+' : this.getMonth()+1,  'd+' : this.getDate(),  'h+' : this.getHours(),  'i+' : this.getMinutes(),  's+' : this.getSeconds(),  'q+' : Math.floor((this.getMonth()+3)/3),  'ms' : this.getMilliseconds() } if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length)); for(var k in type) {  if(new RegExp('('+ k +')').test(format)) {   format = format.replace(RegExp.$1, RegExp.$1.length==1 ? type[k] : ('00'+ type[k]).substr((''+ type[k]).length));  } } return format;}// 添加日期,對應參數分別是:類型(y-年, q-季, m-月, w-周, d-日, h-時, i-分, s-秒)和增加的值Date.prototype.addDate = function(type, num) { var type = type.toLowerCase(); switch (type) {  case 's' : return new Date.parse(Date.parse(this) + (1000 * num));  case 'i' : return new Date.parse(Date.parse(this) + (60000 * num));  case 'h' : return new Date(Date.parse(this) + (3600000 * num));  case 'd' : return new Date(Date.parse(this) + (86400000 * num));  case 'w' : return new Date(Date.parse(this) + ((86400000 * 7) * num));  case 'm' : return new Date(this.getFullYear(), (this.getMonth()) + num, this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds());  case 'q' : return new Date(this.getFullYear(), (this.getMonth()) + num * 3, this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds());  case 'y' : return new Date((this.getFullYear() + num), this.getMonth(), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds()); }}// 計算兩個日期Date.prototype.dateDiff = function(type, date) { if (typeof date == 'string') date = date.toDate(); switch (type) {  case 's' : return parseInt((date - this) / 1000);  case 'i' : return parseInt((date - this) / 60000);  case 'h' : return parseInt((date - this) / 3600000);  case 'd' : return parseInt((date - this) / 86400000);  case 'w' : return parseInt((date - this) / (86400000 * 7));  case 'm' : return (date.getMonth() + 1) + ((date.getFullYear() - this.getFullYear()) * 12) - (this.getMonth() + 1);  case 'y' : return date.getFullYear() - this.getFullYear(); }}// 判斷對象是否是數組Object.prototype.isArray = function() {return Object.prototype.toString.apply(this) == '[object Array]';}// 判斷數組內是否存在指定的元素Array.prototype.inArray = function (value) { if (this.length < 2) return this[0] == value; this.sort(function(a) {  return new RegExp('^' + value).test(a) ? -1 : 1; }); return this[0] == value;}// 在數組中查找元素并返回第一次出現的位置索引,未找到則返回-1。Array.prototype.indexOf = function(string) { var len = this.length, i = 0; if (len < 2) return this[0] == value ? 0 : -1; for (i; i < len; i++) {  if (this[i] == string) return i; } return -1;}// [1, 2, 3].each(function(x) {return x+1}) 得到2, 3, 4Array.prototype.each = function(c) { var ret = []; for(var i = 0; i < this.length; i++) {  ret.push(c(this[i])); } return ret;}// [1, -1, 2].any(function(x) {return x < 0}) 判斷是否數組中有一個元素小于0Array.prototype.any = function(c) { for(var i = 0; i < this.length; i++) {  if (c(this)) return true; } return false;}// [1, 2, 3].all(function(x) {return x > 0}) 判斷是否數組中所有的元素都大于0Array.prototype.all = function(c) { for(var i = 0; i < this.length; i++) {  if (!c(this)) return false; } return true;}// 移除數組指定的元素,如果指定了limit,則僅移除limit個指定元素,如果省略limit或者其值為0,則所有指定元素都會被移除。Array.prototype.unset = function(string, limit) { var len = this.length, i = 0, count = 0; for (i; i < len; i++) {  if (this[i] == string) {   this.splice(i, 1);   if (limit && limit > 0) {    count++;    if (count == limit) break;   }  } } return this;}// 移除數組中重復的元素Array.prototype.unique = function() { var arr = tmp = [], i, len = this.length; if (len < 2) return this; for (i = 0; i < len; i++) {  if (tmp[this[i]]) {   arr.push(this[i]);   tmp[this[i]] = true;  } } return arr;}Array.prototype.min = function() {return Math.min.apply(null, this)} // 求數組中最小值Array.prototype.max = function() {return Math.max.apply(null, this)} // 求數組中最大值

更多關于JavaScript擴展相關內容感興趣的讀者可查看本站專題:《JavaScript擴展技巧總結

希望本文所述對大家JavaScript程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 米易县| 资兴市| 西充县| 蓝山县| 佛冈县| 吉首市| 资源县| 喀喇沁旗| 木兰县| 海盐县| 合江县| 岐山县| 瑞昌市| 湟源县| 涟水县| 安顺市| 乐都县| 鹤庆县| 邵阳市| 许昌县| 平邑县| 桐柏县| 游戏| 德江县| 政和县| 天镇县| 中西区| 资阳市| 永宁县| 马尔康县| 新余市| 渝中区| 宁波市| 千阳县| 拉孜县| 娱乐| 垫江县| 南陵县| 吴忠市| 临清市| 丰台区|