Js prototype Extension Method (string, date, Array Extension), jsprototype
This article describes how to implement prototype extension in js. We will share this with you for your reference. The details are as follows:
String. prototype. isEmpty = function () {return! (/.? [^/S] + /. test (this);} // check whether the String is null // Replace the String. prototype. reserve = function (type) {if (type = 'int') return this. replace (/^/d/g, ''); // replace all characters except numbers in the string else if (type = 'en') return this. replace (/[^ A-Za-z]/g, ''); // replace all characters except English in the string else if (type = 'cn ') return this. replace (/[^/u4e00-/u9fa5/uf900-/ufa2d]/g, ''); // replace all characters except Chinese characters in the string else return this ;} // String inversion String. prototype. reverse = fun Ction () {return this. split (''). reverse (). join ('');} // calculate the length of a String by a Chinese character. prototype. cnLength = function () {return this. replace (/[^/x00-/xff]/g ,'**'). length;} // Replace the space String in the String. prototype. trim = function (type, char) {var type = type? Type. toUpperCase (): ''; switch (type) {case 'B': // replace all the characters to be cleared. If no char is defined, return this with a space by default. replace (char? New RegExp (char, 'G'):/(/s + |)/g, ''); case 'O ': // replace two or more consecutive characters to be cleared with one character. If no char is defined, the return char? This. replace (new RegExp (char + '{2,}', 'G'), char): this. replace (/[/s] {2,}/g, ''); case 'l': // replace to clear characters on the left, if char is not defined, return this is replaced by space by default. replace (char? New RegExp ('^ (' + char + ') *', 'G'):/^ (/s |) */g, ''); case 'r ': // except for characters to be cleared on the right side, if char is not defined, the space is replaced by return this by default. replace (char? New RegExp ('+ char +') * $ ', 'G'):/(/s |) * $/g, ''); default: // remove the characters on both sides of the string to be cleared. If no char is defined, the space is replaced by return this by default. replace (char? New RegExp ('^ (' + char + ') * | (' + char + ') * $', 'G'):/(^/s * |) | (|/s * $)/g, '') ;}// determines whether the String is a numeric String. prototype. isNumer = function (flag) {if (isNaN (this) {return false;} switch (flag) {case '+': return/(^/+? | ^/D ?) /D */.? /D + $/. test (this); // positive case '-': return/^-/d */.? /D + $/. test (this); // negative case 'I': return/(^ -? | ^/+? |/D)/d + $/. test (this); // integer case '+ I': return/(^/d + $) | (^/+? /D + $ )/. test (this); // positive integer case '-I': return/^-/d + $ /. test (this); // negative integer case 'F': return/(^ -? | ^/+? | ^/D ?) /D */./d + $/. test (this); // floating point case '+ F': return/(^/+? | ^/D ?) /D */. /d + $ /. test (this); // Positive floating point number case '-F': return/^-/d */. /d $ /. test (this); // negative floating point number default: return true; // default} // PHP-like 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 ;}// obtain the value of the url parameter 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;} // determine whether it is a date format (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;} // convert String to date 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;} // format the Date, 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 ;}// add date, corresponding parameters are: type (y-year, q-quarter, m-month, w-week, d-Day, h-hour, I-minute, s-second) and the added value 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 () ;}// calculate two dates. prototype. dateDiff = function (type, date) {if (typeof date = 'string') date = date. toDate (); switch (type) {case's ': return parseInt (date-this)/1000); case' I ': return p ArseInt (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 () ;}}// determines whether the Object is an array Object. proto Type. isArray = function () {return Object. prototype. toString. apply (this) = '[object Array]';} // checks whether the specified element Array exists. prototype. inArray = function (value) {if (this. length <2) return this [0] = value; this. sort (function (a) {return new RegExp ('^' + value ). test ()? -1: 1;}); return this [0] = value;} // search for elements in the array and return the location index that appears for the first time. If not, return-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}) returns 2, 3, and 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}) checks whether an element in the array is smaller than 0Array. prototype. any = function (c) {for (var I = 0; I <this. length; I ++) {if (c (t His) return true;} return false;} // [1, 2, 3]. all (function (x) {return x> 0}) determines whether all elements in the array are greater than 0Array. prototype. all = function (c) {for (var I = 0; I <this. length; I ++) {if (! C (this) return false;} return true;} // removes the specified elements from the array. If limit is specified, only the specified limit elements are removed, if limit is omitted or its value is 0, all specified elements will be removed. 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 ;} // remove the repeated element Array from the 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)} // calculates the minimum value of Array. prototype. max = function () {return Math. max. apply (null, this)} // calculates the maximum value in the array.