This article mainly introduces detailed information about Array object extension and String object extension in JS. You can refer to the Code extension code of array object extension, the Code is as follows:
/*** Created by laixiangran on 2016/01/07. * Array Extension */(function () {// traverses the 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, then, put the result into the new Array and 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 (context, this [k], k, this) ;}} return arr ;};} // place the elements that meet the conditions in a new Array and return if (typeof Array. prototype. filter! = "Function") {Array. prototype. filter = function (fn, context) {var arr = []; if (typeof fn = "function") {for (var k = 0, length = this. length; k <length; k ++) {fn. call (context, this [k], k, this) & arr. push (this [k]) ;}} return arr ;};/// if each element in the array can pass the test of the given function, true is returned, otherwise, 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; passed = !! Fn. call (context, this [k], k, this) ;}} return passed ;}/// similar to the every function, however, if one test passes the given function, true if (typeof Array. prototype. some! = "Function") {Array. prototype. some = 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. If no value exists,-1 is returned, from 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 in this & this [I] = item) {return I} return-1};} // returns the index of the element in the Array. if no index exists,-1 is returned, 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 in sequence, and return a value (from left to right) if (typeof Array. prototype. reduce! = "Function") {Array. prototype. reduce = function (callback, 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 element call the given function in sequence and return a value (from the right To the left) if (typeof Array. prototype. reduceRight! = "Function") {Array. prototype. reduceRight = function (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 Returns the new Array if (typeof Array. prototype. uniq! = "Function") {Array. prototype. uniq = function () {var arr = []; arr [0] = this [0]; for (var I = 1; I <this. length; I ++) {if (arr. indexOf (this [I]) =-1) {arr. push (this [I]) ;}} return arr ;}}// specify to delete a value in the Array if (typeof Array. prototype. remove! = "Function") {Array. prototype. remove = function (item) {for (var I = this. length; I> = 0; I --) {if (item = this [I]) {this. splice (I, 1) ;}} return this ;}// disrupt the 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;} return this ;};} // calculate the maximum value of the Array if (typeof Array. prototype. max! = "Function") {Array. prototype. max = function () {return Math. max. apply ({}, this) };}// evaluate the minimum value of the Array if (typeof Array. prototype. max! = "Function") {Array. prototype. min = function () {return Math. min. apply ({}, this) };}// determine whether it is an Array if (typeof Array. prototype. isArray! = "Function") {Array. prototype. isArray = function () {return Object. prototype. toString. apply (this) = "[object Array]" ;}}} ();
The following code expands the string object:
/*** Created by laixiangran on 2015/12/12. * String extension */(function () {// Regular Expression var reg =/^ # ([0-9a-fA-f] {3} | [0-9a-fA-f] {6}) $ /; // Convert RGB color to hexadecimal if (typeof String. prototype. rgbToHex! = "Function") {String. prototype. rgbToHex = function () {var that = this; if (/^ (rgb | RGB )/. test (that) {var aColor = that. replace (/(?: \ (| \) | Rgb | RGB) */g ,""). split (","); var strHex = "#"; for (var I = 0; I <aColor. length; I ++) {var hex = Number (aColor [I]). toString (16); 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]);} return numHex ;}} else {return that ;}};} // convert hexadecimal color to RGB format if (typeof String. prototype. hexToRgb! = "Function") {String. prototype. hexToRgb = function () {var sColor = this. toLowerCase (); if (sColor & 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;} // process the six-digit color value var sColorChange = []; for (var j = 1; j <7; j + = 2) {sColorChange. push (parseInt ("0x" + sColo R. slice (j, j + 2);} return "RGB (" + sColorChange. join (",") + ")" ;}else {return sColor ;};}// remove the white space at the beginning and end of the String if (typeof String. prototype. trim! = "Function") {String. prototype. trim = function () {return this. replace (/^ \ s + | \ s + $/g ,"");};}}());