JavaScript advanced Programming-Object, Array (stack method, queue method, reorder method, iterative method)

Source: Internet
Author: User

1. Defining objects using object literalsvar person={}; When you create an object this way, you do not actually call the object constructor. developers prefer the syntax of object literals.   2. Sometimes when you need to pass a large number of optional parameters, you typically use object literals to encapsulate multiple optional parameters.   3. Differences between the point representation of object properties and square brackets notation(1) Functional: No difference between the two(2) but a bit of square brackets can be accessed by a variable to access the propertyFor example:var person={Name: "Nic"}dot notation: Person.nameSquare bracket notation: var prop= "name";Person[prop] (3) There is also an advantage:If the property name contains a character or keyword that causes a syntax error, the use of square brackets is not correct when the word is reservedFor example: person["First Name"]= "OK";(4) in general, the use of dot notation is recommended  4. Problems with creating arrays
var colors=[1,2,]    // don't do that. This creates an array of 2 or 3 items var opy=[,,,,,]    // don't do that. This creates an array with 5 or 6 items.
this is because IE8 and previous versions have bugs in implementing array literals Array constructors are not called when creating arrays using literals  5. If you set a value for an index that exceeds the number of existing entries in the array. such as: Var color=[1,2,3]Color[3], the array is automatically incremented to the length of the index value plus 1at this point, the value of color[3] is undefined  6. The length of the array is not just read-only. By setting the length of this property, you can continuously add new items to the end of the array.   7. Array converted to string toString () join ()
Array.tostring ()    // returns a comma-delimited string array.valueof ()    // returned or an array array.join ( ",")    /// can also

8. Stack method of the array push () pop ()A stack is a data structure in which the most recently added item is first removed (last in, first out). The insertion and removal of items in the stack occurs only in one place-the top of the stack. ECMAScript provides a push () and Pop () method to implement this stack. The push () method adds one or more elements to the end of the array and returns the new length. the Pop () method is used to delete and return the last element of the array. Example:
var arr=[]; var count=arr.push (' A ', ' B ');    // count=2arr.push (' C '); var item=arr.pop ();    // remove the last item C  item=c and change the array length

9. Queue Method Shift () Unshift ()access rules for queue data are FIFOECMAScript provides a shift () to implement. The Shift () method removes the first element from the array and returns the value of the first element.
The unshift () method adds one or more elements to the beginning of the array and returns the new length.   10. Reorder Method Sort () reverse ()ECMAScript provides a sort () with reverse () to implement. Sort () calls the ToString () method of each array item, comparing the resulting string to sort.    11. Array of stitching concat ()

The Concat () method is used to concatenate two or more arrays.

The method does not alter the existing array, but only returns a copy of the concatenated array.

The 12.slice () method returns the selected element from an existing array.   13. Location method: IndexOf () and LastIndexOf ()  14. Iterative MethodsECMASCRIPT5 defines the following 5 methods, all 5 of which receive three parameters: the value of the array item, the position of the item in the array, the array object itself every (), filter (), ForEach (), map (), some ()Example:
varnum=[1,2,3,4]; varRes=num.every (function(Item,index,array) {return(item>2)})    //false must be greater than 2 for each item to return True varRes=num.some (function(Item,index,array) {return(item>2)})    //true if there is a greater than 2, returns true varRes=num.filter (function(Item,index,array) {return(item>2)})    //[3,4]varRes=num.foreach (function(Item,index,array) {return(item>2)})    //[1,4,9,16]

     

JavaScript advanced Programming-Object, Array (stack method, queue method, reorder method, iterative method)

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.