From the worry-free script collection, some are indeed good, more practical, it is worth collecting.
How to extend the prototype object of Array
Copy codeThe Code is as follows: // Delete the data in the array
Array. prototype. del = function (n)
{
If (n <0) return this;
Return this. slice (0, n). concat (this. slice (n + 1, this. length ));
}
// Shuffling the Array
Array. prototype. random = function ()
{
Var nr = [], me = this, t;
While (me. length> 0)
{
Nr [nr. length] = me [t = Math. floor (Math. random () * me. length)];
Me = me. del (t );
}
Return nr;
}
// Sort the number Array
Array. prototype. sortNum = function (f)
{
If (! F) f = 0;
If (f = 1) return this. sort (function (a, B) {return B-;});
Return this. sort (function (a, B) {return a-B ;});
}
// Obtain the numeric entry of the number Array
Array. prototype. getMax = function ()
{
Return this. sortNum (1) [0];
}
// Obtain the minimum entry of the number Array
Array. prototype. getMin = function ()
{
Return this. sortNum (0) [0];
}
// The position where the specified Element value appears for the first time in the array
Array. prototype. indexOf = function (o)
{
For (var I = 0; I <this. length; I ++) if (this [I] = o) return I;
Return-1;
}
// Remove repeated items from the array
Array. prototype. removeRepeat = function ()
{
This. sort ();
Var rs = [];
Var cr = false;
For (var I = 0; I <this. length; I ++)
{
If (! Cr) cr = this [I];
Else if (cr = this [I]) rs [rs. length] = I;
Else cr = this [I];
}
Var re = this;
For (var I = rs. length-1; I> = 0; I --) re = re. del (rs [I]);
Return re;
}
Example:
Var arr = ["ni", "wo", "ta"];
Delete "wo" from the array"
Var newArr = arr. del (1 );
Returns the position where "me" appears for the first time in the array. If not,-1 is returned.
Var strPos = arr. indexOf ("me ");
Method for extending the String prototype object
Copy codeThe Code is as follows: // get the character array
String. prototype. ToCharArray = function ()
{
Return this. split ("");
}
// Obtain N identical strings
String. prototype. Repeat = function (num)
{
Var tmpArr = [];
For (var I = 0; I <num; I ++) tmpArr. push (this );
Return tmpArr. join ("");
}
// Reverse Order
String. prototype. Reverse = function ()
{
Return this. split (""). reverse (). join ("");
}
// Test whether it is a number.
String. prototype. IsNumeric = function ()
{
Var tmpFloat = parseFloat (this );
If (isNaN (tmpFloat) return false;
Var tmpLen = this. length-tmpFloat.toString (). length;
Return tmpFloat + "0". Repeat (tmpLen) = this;
}
// Test whether the value is an integer.
String. prototype. IsInt = function ()
{
If (this = "NaN") return false;
Return this = parseInt (this). toString ();
}
// Merge multiple blanks into one blank
String. prototype. resetBlank = function ()
{
Return this. replace (/s +/g ,"");
}
// Remove the left blank
String. prototype. LTrim = function ()
{
Return this. replace (/^ s +/g ,"");
}
// Remove the right Blank
String. prototype. RTrim = function ()
{
Return this. replace (/s + $/g ,"");
}
// Remove the blank spaces on both sides
String. prototype. trim = function ()
{
Return this. replace (/(^ s +) | (s + $)/g ,"");
}
// Retain numbers
String. prototype. getNum = function ()
{
Return this. replace (/[^ d]/g ,"");
}
// Retain letters
String. prototype. getEn = function ()
{
Return this. replace (/[^ A-Za-z]/g ,"");
}
// Retain Chinese Characters
String. prototype. getCn = function ()
{
Return this. replace (/[^ u4e00-u9fa5uf900-ufa2d]/g ,"");
}
// Get the byte length
String. prototype. getRealLength = function ()
{
Return this. replace (/[^ x00-xff]/g, "--"). length;
}
// Extract a string of the specified length from the left
String. prototype. left = function (n)
{
Return this. slice (0, n );
}
// Extract a string of the specified length from the right
String. prototype. right = function (n)
{
Return this. slice (this. length-n );
}
// HTML Encoding
String. prototype. HTMLEncode = function ()
{
Var re = this;
Var q1 = [/x26/g,/x3C/g,/x3E/g,/x20/g];
Var q2 = ["&", "<", ">", ""];
For (var I = 0; I <q1.length; I ++)
Re = re. replace (q1 [I], q2 [I]);
Return re;
}
// Unicode Conversion
String. prototype. ascW = function ()
{
Var strText = "";
For (var I = 0; I <this. length; I ++) strText + = "" + this. charCodeAt (I) + ";";
Return strText;
}