js實現(xiàn)prototype擴展的方法(字符串,日期,數(shù)組擴展)
更新時間:2016年01月14日 15:09:49 作者:乘著風在飛
這篇文章主要介紹了js實現(xiàn)prototype擴展的方法,實例分析了JavaScript針對字符串、日期、數(shù)組等的prototype擴展相關技巧,需要的朋友可以參考下
本文實例講述了js實現(xiàn)prototype擴展的方法。分享給大家供大家參考,具體如下:
String.prototype.isEmpty = function () { return !(/.?[^/s ]+/.test(this)); } //檢測字符串是否為空 // 替換字符 String.prototype.reserve = function(type) { if (type == 'int') return this.replace(/^/d/g, ''); // 替換字符串中除了數(shù)字以外的所有字符 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' : // 將兩個以上的連續(xù)欲清除字符替換為一個,未定義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, ''); } } // 判斷字符串是否是數(shù)字 String.prototype.isNumer = function(flag) { if (isNaN(this)) {return false;} switch (flag) { case '+' : return /(^/+?|^/d?)/d * /.?/d+$/.test(this); // 正數(shù) case '-' : return /^-/d * /.?/d+$/.test(this); // 負數(shù) case 'i' : return /(^-?|^/+?|/d)/d+$/.test(this); // 整數(shù) case '+i' : return /(^/d+$)|(^/+?/d+$)/.test(this); // 正整數(shù) case '-i' : return /^-/d+$/.test(this); // 負整數(shù) case 'f' : return /(^-?|^/+?|^/d?)/d * /./d+$/.test(this); // 浮點數(shù) case '+f' : return /(^/+?|^/d?)/d * /./d+$/.test(this); // 正浮點數(shù) case '-f' : return /^-/d * /./d$/.test(this); // 負浮點數(shù) default : return true; // 缺省 } } // 仿PHP的str_pad String.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對應參數(shù)的值 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; } // 添加日期,對應參數(shù)分別是:類型(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(); } } // 判斷對象是否是數(shù)組 Object.prototype.isArray = function() {return Object.prototype.toString.apply(this) == '[object Array]';} // 判斷數(shù)組內(nèi)是否存在指定的元素 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; } // 在數(shù)組中查找元素并返回第一次出現(xiàn)的位置索引,未找到則返回-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, 4 Array.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}) 判斷是否數(shù)組中有一個元素小于0 Array.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}) 判斷是否數(shù)組中所有的元素都大于0 Array.prototype.all = function(c) { for(var i = 0; i < this.length; i++) { if (!c(this)) return false; } return true; } // 移除數(shù)組指定的元素,如果指定了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; } // 移除數(shù)組中重復的元素 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)} // 求數(shù)組中最小值 Array.prototype.max = function() {return Math.max.apply(null, this)} // 求數(shù)組中最大值
更多關于JavaScript擴展相關內(nèi)容感興趣的讀者可查看本站專題:《JavaScript擴展技巧總結》
希望本文所述對大家JavaScript程序設計有所幫助。
您可能感興趣的文章:
- 判斷js中各種數(shù)據(jù)的類型方法之typeof與0bject.prototype.toString講解
- js類定義函數(shù)時用prototype與不用的區(qū)別示例介紹
- JavaScript使用prototype定義對象類型
- JavaScript使用prototype定義對象類型(轉)[
- JavaScript類和繼承 prototype屬性
- JavaScript子類用Object.getPrototypeOf去調(diào)用父類方法解析
- javascript基于prototype實現(xiàn)類似OOP繼承的方法
- JS偽繼承prototype實現(xiàn)方法示例
- 淺談js構造函數(shù)的方法與原型prototype
- Javascript中prototype屬性實現(xiàn)給內(nèi)置對象添加新的方法
- JS利用prototype給類添加方法操作詳解
相關文章
javascript事件循環(huán)event loop的簡單模型解釋與應用分析
這篇文章主要介紹了javascript事件循環(huán)event loop的簡單模型解釋與應用,結合實例形式分析了javascript事件循環(huán)event loop的與模型原理及使用技巧,需要的朋友可以參考下2020-03-03