An Array Extension in the return of the Javascript king

Source: Internet
Author: User

I have been reading this book recently. It is recommended that you want to learn about JS shoes and buy a book.

// Any is a set iteration function that accepts a closure as a parameter // when any element in the Set calls the closure result and returns a non-false result, any () returns the calculation result, otherwise, falsearray is returned. prototype. any = function (closure, _ set) {// The second parameter is a set of processing computing results. // The end of this design is to reuse any_set = _ set | false in the each method; // if the closure parameter is not defined if (typeof closure = 'undefined') {// function specified to return the value of the array element closure = function (X) {return x };}; // if the closure parameter is not the function if (typeof closure! = 'Function') {// then it should be converted to a function var c = closure; closure = function (x) that returns this value) {return x = C };}; // convert the first 3rd parameters to an array, because these values will also be used as the parameter var ARGs = array during iteration. apply (this, arguments ). slice (2); // cyclically traverse each element in the array for (VAR I = 0; I <this. length; I ++) {// call the closure var rval = closure referenced by the closure parameter with the custom parameter and the subscript I of the array. apply (this, [this [I]. concat (ARGs ). concat (I); // if the return value is converted to boolean, It is 'true' and is not a value of 0if (rval | rval = 0) {// If _ set exists, put the calculation result into the _ set if (_ set & _ Set. put) {_ set. put (rval);} else {// otherwise, return the return rval ;}; // each is a set iteration function that accepts a closure as a parameter and a set of optional parameters // This iteration function calculates each element and optional parameters of the set using the closure in sequence, return the calculated result set to array. prototype. each = function (closure) {closure = closure | undefined; VaR _ set = []; _ set. put = _ set. push; return this. any. apply (this, [closure, _ set]. concat (array. apply (this, arguments ). slice (1);} // returns tr only when every element in the Set calls the closure function and returns true. Uearray. prototype. all = function (closure) {return this. each. apply (this, arguments ). length = This. length;} // remove the null and false elements in the array. prototype. trim = function () {return this. each ();} // determines whether the array contains an element array. prototype. contains = function (EL) {return this. any (function (x) {return x = El});} // returns the first index with the value of the array equal to El. If it does not exist, returns-1array. prototype. indexof = function (EL) {return this. any (function (x, I) {return El = x? I:-1});} // obtain the sub-array from start to end. prototype. subarr = function (START, end) {end = end | math. infinity; return this. each (function (x, I) {return I> = Start & I <End? X: NULL});} // This Is A set iteration function. It accepts a list and a closure. // it returns a closure that matches the array of the set and list elements. prototype. map = function (list, closure) {If (typeof list = 'function' & typeof closure! = 'Function') {var li = closure; closure = List; List = Li ;}; closure = closure | array; return this. each (function (x, I) {return closure (x, list [I])});}

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.