Advanced programming practices for arrays in JavaScript-2

Source: Internet
Author: User

let's see . EcmaScript5 The array of new APIs in the specification, they are very useful,

In the end of this section, we will use array array as an object to construct a ArrayList class similar to that in Java.

To facilitate the encapsulation of common logic for code reuse.


Api:

/** @param {function} callback@param {Object} [InitialValue] @return {object}*/array.prototype.reduce = function ( Callback,initialvalue) {};/** @param {Function} callback@param {Object} [InitialValue] @return {object}*/ Array.prototype.reduceRight = function (callback,initialvalue) {};/** @param {Object} searchelement@param {Number} [ FromIndex] @return {number}*/array.prototype.indexof = function (searchelement,fromindex) {};/** @param {Object} Searchelement@param {Number} [FromIndex] @return {number}*/array.prototype.lastindexof = function (Searchelement, FromIndex) {};/** @param {Function} callback@param {Object} [Thisobject] @return {boolean}*/array.prototype.every = function (callback,thisobject) {};/** @param {Function} callback@param {Object} [Thisobject] @return {array}*/ Array.prototype.filter = function (callback,thisobject) {};/** @param {function} callback@param {Object} [thisobject]@ return {Void}*/array.prototype.foreach = function (callback,thisobject) {};/** @param {function} callback@param {OBJECT} [Thisobject] @return {array}*/array.prototype.map = function (callback,thisobject) {};/** @param {function} Callback@param {Object} [Thisobject] @return {boolean}*/array.prototype.some = function (callback,thisobject) {};/**@ param {Object} object@return {Boolean}*/array.prototype.isarray = function (object) {};

Use:

/** * @class MyEcmaScript5 * @description * @time 2014-09-16 21:38 * @author Starzou **///defines an array that stores elements of different data types var array = [8, 2 , 1, 5], array2 = [[0, 1], [1, 2], [2, 3]], value;///Array.prototype.reduce = function (ca  Llback,initialvalue) {};//dissolve array://Call the reduce method to provide a callback function,//Call the array of lenght-1 times callback function//First time previousvalue for initialvalue//thereafter The return value of the reduce method, as previousvalue//Reduceright, begins at the far right of the array, with the elements in the above//array Added value = Array.reduce (function (Previousvalue,    CurrentValue, index, array) {return previousvalue + CurrentValue;  }); Console.log (value); 16//array Flattening value = array2.reduce (function (Previousvalue, CurrentValue, index, array) {return Previousvalue.conc    at (CurrentValue); }); Console.log (value); [0, 1, 1, 2, 2, 3]///Array.prototype.indexOf = function (searchelement,fromindex) {};//looking backwards from the FromIndex index Searcheleme The NT element finds and returns its index, otherwise returns -1//fromIndex default to 0console.log (Array.indexof (1)); 2console.log (Array.indexof (3)); -1///Array.prototype. lastIndexOf = function (searchelement,fromindex) {};//looks forward from the FromIndex index to the searchelement element to find and return its index, otherwise returns -1console.log ( Array.lastindexof (1)); 2console.log (Array.lastindexof (3)); -1///Array.prototype.every = function (callback,thisobject) {};//All elements of the test array have passed the test of the specified function = Array.every ( function (element, index, array) {return element > 1;}); Console.log (value); false///Array.prototype.filter = function (callback,thisobject) {};//array filter://Returns the element tested by the function (when the callback function returns True, indicating that the element passed the test), Generate a new array value = Array.filter (function (element, index, array) {return element > 1;}); Console.log (value);//[8, 2, 5]///Array.prototype.forEach = function (callback,thisobject) {};//Executes the callback function once for each array element. Array.foreach (function (element, index, array) {Console.log (element);}); Array.prototype.map = function (callback,thisobject) {};//array mapping://Returns a new array consisting of the return values of each element in the original array that are called after a specified method. Value = Array.map (function (element, index, array) {return element * element;}); Console.log (value); [64, 4, 1, 25]///Array.prototype.some = function (callback,thisobject) {};//tests whether certain elements in the array have passed the specified function. Value = Array.some (function (element, index, array) {return element > 1;}); Console.log (value); true///Array.prototype.isArray = function (object) {};//Determines whether an element is an array type Console.log (Array.isarray (Array));//True

Operation Result:




Advanced programming practices for arrays in JavaScript-2

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.