Advanced programming practices for arrays in JavaScript

Source: Internet
Author: User
Tags array sort

Today we present a comprehensive introduction to the advanced use of arrays in JavaScript, with the ECMASCRIPT5 array API in combat.

Leveraging these new APIs and tips will improve your development efficiency and the level of code.

Understanding these native APIs is very necessary, and in time, we can also write underscore ... Such a library of tools to come.

Come on baby!


First look at Array.prototype's family photo.



In JavaScript, arrays are stored sequentially in a series of values, and the length is dynamically enlarged.

, let's look at the definition of Array in our EcmaScript specification.

/** @param {... *} [args] @constructor */function Array (args) {}/** @param {... *} [items] @return {array}*/ Array.prototype.concat = function (items) {};/** @param {string} [separator] @return {string}*/array.prototype.join = function (separator) {};/** @return {*}*/array.prototype.pop = function () {};/** @param {... *} [items] @return {number}*/ Array.prototype.push = function (items) {};/** @return {array}*/array.prototype.reverse = function () {};/** @return {*}*/ Array.prototype.shift = function () {};/** @param {number} [start] @param {number} [end] @return {array}*/ Array.prototype.slice = function (start,end) {};/** @param {function} [Comparefn] @return {array}*/array.prototype.sort = function (comparefn) {};/** @param {number} [start] @param {number} [DeleteCount] @param {... *} [items] @return {array}*/ Array.prototype.splice = function (start,deletecount,items) {};/** @param {... *} [items] @return {number}*/ Array.prototype.unshift = function (items) {};/** @return {array}*/array.prototype.valueof = function () {};


Inall the array objects in JavaScript are derived from the array constructor and share the methods on the Array.prototype, so let's see

How the method on the array prototype is used.


/** * @class myecmascript * @description * @time 2014-09-13 11:52 * @author starzou **///define variable var obj = {name: ' obj-1 '}, SA    Yhello = function (name) {console.log (' Hello%s guy ', name); }, arr1 = [5, 6], array = [3, ' 2 ', 1, 1, True, SayHello, ARR1, 9], i;////array of methods used, divided into 2 classes///1, does not change the original array method//ARRAY.PR Ototype.concat = function (items) {}; The array is merged to produce a new array console.log (Array.concat (arr1), array); [3, ' 2 ', 1, 1, true, [function], [5, 6], 9, 5, 6] [3, ' 2 ', 1, 1, true, [function], [5, 6], 9]//Array.prototype . join = function (separator) {}; array concatenation, resulting in a new string Console.log (Arr1.join ('-')); 5-6//Array.prototype.slice = function (start,end) {}; Array segmentation, starting from the start position (subscript starts at 0), excluding the end position, cut into a new array (that is, the length of the new array = End-start) console.log (Array.slice (0, 3));  [3, ' 2 ', 1]///2, the method that will change the original array//Array.prototype.pop = function () {}; pops up the last element of the array and returns the element Console.log (Array.pop ()); 9//Array.prototype.push = function (items) {}; Adds an element at the end of the array and returns the length of the array Console.log (Array.push (arr1)); 8//Array.prototype.reverse= function () {}; Reverses the array element Console.log (Array.reverse ()); [[5, 6], [5, 6], [Function], True, 1, 1, ' 2 ', 3]//Array.prototype.shift = Function () {}; Removes the first element of the divisor group and returns the element Console.log (Array.shift (), '---', array); [5, 6] '---' [[5, 6], [Function], True, 1, 1, ' 2 ', 3]//Array.prototype.unshift = Function (items) {}; Adds an element in the first position of the array and returns the length of the array Console.log (Array.unshift (110)); 8//Array.prototype.sort = function (Comparefn) {}; Array sort Console.log (Array.Sort ()); Default ascending sort, [1, 1, 2 ', 3, [5, 6], [function], True]console.log (Array.Sort (function (one, both) {return one &lt ; })); Custom sorting (when customizing, consider comparisons between different types of elements), [3, ' 2 ', 1, 1, [Function], [5, 6], true]//Array.prototype.splice = Function (st Art,deletecount,items) {}; The most powerful method of the array//removes the DeleteCount element from the start position and adds items to the start position, returning the deleted element array Console.log (Array.splice (0, 7, 119), array); [3, ' 2 ', 1, 1, [Function], [5, 6]] [119, True]
Operation Result:





Knowing how the above array is done, let's look at the array of new APIs in the ECMASCRIPT5 specification, and how to apply it to practice.

/** @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) {};


Not to be continued ...

Advanced programming practices for arrays 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.