Detailed description of Array object extension and String object extension in JS, arraystring

Source: Internet
Author: User
Tags shuffle javascript array hasownproperty

Detailed description of Array object extension and String object extension in JS, arraystring

If you don't talk much about it, just add the array object extension code to everyone. The specific 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, falseif (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, as long as a test passes the given function, trueif (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 right to 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 the repeated items (uniqueness) and return 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" + sColor. slice (j, j + 2);} return "RG B ("+ 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 ,"");};}}());
Articles you may be interested in:
  • Set of javascript String extension methods
  • How to extend HTML encoding and decoding for Javascript String objects
  • JavaScript Array Extension implementation code
  • Javascript string extension library code
  • Javascript Array object extension function code
  • Explore and solve problems caused by extension of Array. prototype. indexOf for JS
  • Js Array object extension function code
  • JavaScript Array object extension indexOf () method
  • Example of extending the Array contains method in JavaScript

Related Article

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.