A detailed explanation of the array object extension and string object extension _javascript techniques in JS

Source: Internet
Author: User
Tags shuffle hasownproperty

Nonsense not to say more, directly to everyone on the array object extension code, the specific code as follows:

/** * Created by Laixiangran on 2016/01/07. * Array extension/function () {//traversal array if (typeof Array.prototype.forEach!= "function") {Array.prototype.forEach = function (FN, context) {for (var i = 0; i < this.length i++) {if typeof fn = = "function" && Object.prototype.hasOwnProperty.call (
This, i) {Fn.call (context, this[i], I, this);}
}; //Let each element in the array call the given function, and then place the resulting result in the new array to return if (typeof Array.prototype.map!= "function") {Array.prototype.map = function (fn , context) {var arr = []; if (typeof fn = = ' function ') {for (var k = 0, length = this.length; k < length; k++) {arr
. push (Fn.call, This[k], K, this);
} return arr;
};  ////(typeof Array.prototype.filter!= "function") {Array.prototype.filter = function (FN, context) by placing qualifying elements in a new array {var arr = []; if (typeof fn = = "function") {for (var k = 0, length = this.length; k < length; k++) {Fn.call (conte
XT, This[k], K, this) && Arr.push (This[k]);
} return arr;
}; //If each element in the array is able to pass the givenIs true, vice versa False if (typeof Array.prototype.every!= "function") {Array.prototype.every = function (FN, context) {
var passed = true; if (typeof fn = = "function") {for (var k = 0, length = this.length; k < length; k++) {if (passed = = false) break; p assed =!!
Fn.call (context, this[k], K, this);
} return passed;
}; //similar to the Every function, but returns True if (typeof Array.prototype.some!= "function") {Array.prototype.some = function as long as there is a test passed the given function (FN  , context) {var passed = false; if (typeof fn = = ' function ') {for (var k = 0, length = this.length; k < length; k++) {if (passed = = true) break; passed =!!
Fn.call (context, this[k], K, this);
} return passed;
};  }//Returns the index of the element in the array, does not return-1, left to right if (typeof Array.prototype.indexOf!= "function") {Array.prototype.indexOf = function (item, Index) {var n = this.length, i = index = = NULL 0:index < 0?
Math.max (0, n + index): index;
for (; i < n; i++) {if (i) && this[i] = = Item {return i}} return-1}; }//ReturnReturns the element to the index of the array, does not return-1, from right to left if (typeof Array.prototype.lastIndexOf!= "function") {Array.prototype.lastIndexOf = function ( Item, index) {var n = this.length, i = index = = null? n-1: Index < 0?
Math.max (0, n + index): index;
for (; I >= 0; i--) {if (I in this && this[i] = = Item) {return i;}} return-1;
}; //Let the array element call the given function sequentially, and finally return a value (Left-to-right) if (typeof Array.prototype.reduce!= "function") {Array.prototype.reduce = function (cal Lback, InitialValue) {var previous = InitialValue, k = 0, length = this.length; if (typeof initialvalue = = "undefined")
{previous = This[0]; k = 1;} if (typeof callback = = "function") {for (k; k < length; k++) {this.hasownproperty (k) && (previous = callback
(Previous, This[k], K, this));
} return previous;
}; //Let the array elements call the given function sequentially, and finally return a value (Right-to-left) if (typeof Array.prototype.reduceRight!= "function") {Array.prototype.reduceRight = Fu
Nction (callback, InitialValue) {var length = this.length, k = length-1, previous = InitialValue;if (typeof InitialValue = = "undefined") {previous = this[length-1]; k--} if (typeof callback = = "function") {for ( K K >-1;
K-=1) {This.hasownproperty (k) && (previous = callback (previous, This[k), K, this);}
return previous;
}; ///Remove duplicates (uniqueness), return the new array if (typeof Array.prototype.uniq!= "function") {Array.prototype.uniq = function () {var arr = []; a
Rr[0] = this[0];
for (var i = 1; i < this.length i++) {if (Arr.indexof (this[i) = = 1) {Arr.push (this[i));} return arr;
}; }//Specifies to delete a value in the array if (typeof Array.prototype.remove!= "function") {Array.prototype.remove = function (item) {for (var i = t His.length; I >= 0;
i--) {if (item = = This[i]) {This.splice (I, 1);}} return this;
}; //Shuffle array order if (typeof Array.prototype.shuffle!= "function") {Array.prototype.shuffle = function () {var i = This.length
;
while (i) {var j = Math.floor (Math.random () *i), var t = this[--i]; This[i] = This[j]; this[j] = t;
}; }//To find the maximum value of an array if (typeof Array.prototYpe.max!= "function") {Array.prototype.max = function () {return Math.max.apply ({}, this)};//Find the minimum value of an array if (typeof Arr 

Ay.prototype.max!= "function") {Array.prototype.min = function () {return Math.min.apply ({}, this)};} Determines whether the array if (typeof Array.prototype.isArray!= "function") {Array.prototype.isArray = function () {return Object.protot
Ype.toString.apply (This) = = "[Object Array]";
}; } 
}());

The following

is the string object extension code as follows:

/** * Created by Laixiangran on 2015/12/12. * String extension/(function () {//16 in the regular expression of the color value var reg =/^# ([0-9a-fa-f]{3}|[
0-9A-FA-F]{6}) $/; RGB color Conversion to 16 if (typeof String.prototype.rgbToHex!= "function") {String.prototype.rgbToHex = function () {var that =
This if (/^ (rgb| RGB)/.test {var acolor = That.replace (/?: \ (|\) |rgb|
RGB) */g, ""). Split (",");
var strhex = "#";  for (var i=0 i<acolor.length; i++) {var hex = number (Acolor[i]). toString (); if (hex = = "0") {hex = hex;} strhex
+ + hex;
} if (Strhex.length!== 7) {strhex = that;} return strhex;
}else if (reg.test (that)) {var anum = That.replace (/#/, ""). Split (""); if (anum.length = = 6) {return that;} else if (anum.length = = 3) {var numhex = "#"; for (var j=0 j<anum.length; J + +) {Numhex = (anum[j]+anum[j));} Retu
RN Numhex;
}else{return to that;}; }//16 convert color to RGB format if (typeof String.prototype.hexToRgb!= "function") {STRING.PROTOTYPE.HEXTORGB = function () {var scol
or = This.tolowercase (); if (Scolor &Amp;& reg.test (Scolor)) {if (scolor.length = = 4) {var scolornew = "#"; for (var i = 1; i < 4; i++) {scolornew =
Scolor.slice (i,i+1). Concat (Scolor.slice (i,i+1));
} Scolor = Scolornew;
//Handle six-bit color value var scolorchange = []; for (var j=1 j<7; j+=2) {Scolorchange.push (parseint ("0x" +scolor.slice (j,j+2))), return "RGB (" + Scolorchange.join (
",") + ")";
}else{return scolor;}; ///Remove the string from end to End if (typeof String.prototype.trim!= "function") {String.prototype.trim = function () {return This.replace ( /^\s+|\s+$/g, "");};} ());

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.