In-depth JavaScript advanced programming objects, arrays (stack method, queue method, re-sorting method, iterative method) _ javascript skills

Source: Internet
Author: User
This article mainly introduces the objects and arrays (stack method, queue method, re-sorting method, iteration method) of JavaScript advanced programming, if you need it, you can refer to inheritance as one of the most talked-about concepts in the OO language. Many OO languages support two inheritance Methods: interface inheritance and implementation inheritance. Interface inheritance only inherits the method signature, while implementation inheritance inherits the actual method. As described above, because the function has no signature, interface inheritance cannot be implemented in ECMAScript. ECMAScript only supports implementation inheritance, and its implementation inheritance mainly relies on the prototype chain.

1. Define an object using the object literal

var person={};

When using this method to create an Object, the Object constructor is not actually called.

Developers prefer the syntax of object literal.

2. When a large number of optional parameters need to be passed, multiple optional parameters are encapsulated using the object literal.

3. Differences between the dot notation and square brackets notation of Object Attributes

(1) functions: there is no difference between the two

(2) but the square brackets can be used to access attributes through variables.

For example:

  var person={  name:"Nic"}

Point representation: person. name

Square brackets: var prop = "name ";
Person [prop]

(3) Another advantage is:

If the attribute name contains characters or keywords that may cause syntax errors, square brackets are not used for reserved words.

For example: person ["first name"] = "OK ";

(4) we recommend that you use dot notation.

4. array creation Problems

Var colors = [1, 2,] // do not. This creates an array containing two or three items.
Var opy = [,] // do not. In this way, an array containing five or six items is created.

This is because IE8 and earlier versions have bugs in implementing the array literal.

The Array constructor is not called when an Array is created using a literal.

5. If the index of a value exceeds the number of existing items in the array.

For example, var color = [1, 2, 3]

Color [3], the array is automatically added to the index value plus the length of 1

In this case, the value of color [3] is undefined.

6. The length of the array is not only read-only. You can add new items to the end of the array by setting the length attribute.

7. Convert the array into a string toString () join ()

Array. toString () // returns a comma-separated string array. valueOf () // returns an array. join (",") // You can also

8. array stack method push () pop ()

Stack is a data structure, that is, the newly added items are first removed (first-in-first-out ). The insertion and removal of items in the stack only happens at one position-the top of the stack.
ECMAScript provides the push () and pop () methods to implement this stack.

The push () method can add one or more elements to the end of an array and return a 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 ()

The access rule for queue data is FIFO
ECMAScript provides shift () for implementation.
Shift () is used to delete the first element of the array from it and return the value of the first element.
The unshift () method can add one or more elements to the beginning of an array and return a new length.

10. resort method sort () reverse ()

ECMAScript provides sort () and reverse () for implementation.

Sort () calls the tostring () method of each array item to sort the obtained strings.

11. array concatenation concat ()

The concat () method is used to connect two or more arrays.

This method does not change the existing array, but only returns a copy of the connected array.

12. The slice () method returns the selected element from an existing array.

13. Location Method: indexOf () and lastIndexOf ()

14. Iteration Method

ECMAScript5 defines the following five methods, which receive three parameters: the value of an array item, the position of the item in the array, and the Array

every(),filter(),forEach(),map(),some()

Example:

Var num = [1, 2, 4]; var res = num. every (function (item, index, array) {return (item> 2)}) // false returns truevar res = num only when each item is greater than 2. some (function (item, index, array) {return (item> 2)}) // true if one is greater than 2, truevar res = num is returned. filter (function (item, index, array) {return (item> 2)}) // [3, 4] var res = num. forEach (function (item, index, array) {return (item> 2)}) // [1, 4, 9, 16]

Iteration Methods in javascript array objects

/* The Iteration Method in the javascript array object * ECMAScript5 defines five Iteration Methods for the array. Each method accepts two parameters. The first is the iteration function, and the second is the scope object of the function. (optional ]. * The function to be iterated accepts three parameters. The first is the value of the element to be iterated in the array, and the second is the position of the element to be iterated in the array, the third is iteration array itself. * 1. every () runs the given function for each item in the array. If this function returns true for each item, true * 2 is returned. filter () is used to run a given function for each item in the array, and returns an array composed of items that return true. * 3. forEach () runs the given function for each item in the array. This method does not return a value * 4. map () runs the given function for each item in the array and returns an array * 5 consisting of the results of each function call. some () runs the given function for each item in the array. If this function returns true for any item, true is returned. ** browsers supported by these iteration methods include IE9 +, firefox2 +, Safari3 +, Opera 9.5 +, chrome */var num = [, 9]; var everyResult = num. every (function (item, index, array) {if (item> 2) {return true ;}}); alert (everyResult); var someResult = num. some (function (item) {if (item> 2) {return true ;}}); alert (someResult); var filterResult = num. filter (function (item) {if (item> 2) {return true ;}}); alert (filterResult); var mapResult = num. map (function (item) {if (item> 2) {return true ;}}); alert (mapResult); var forEachResult = num. forEach (function (item) {if (item> 2) {return true ;}}); alert (forEachResult );

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.