JavaScript Advanced Programming 3 Learning Notes

Source: Internet
Author: User
Tags array length

Reference types are object, Array, Date, RegExp, Function, basic wrapper type

Create an object instance with the new + constructor, for example: var obj = new Object ();

Object type

You can also use object literal notation to represent objects:

var person = {    name : "harry",    age : 29};

Accessing the properties of an object (or using the object's method to manipulate it) uses dot notation or square brackets (for property notation), such as

alert(person["name"]);           //"harry"alert(person.name);               //"harry"
Array type

Two methods for creating arrays, constructors and array literal representations:

    • Constructor: var colors = new Array(3); Alternatively var colors = new Array("red", "blue"); , where the operator new can be omitted, the passed argument is a numeric value representing the number of items in the array, and the passed parameter is another type that represents the value of the array item.

    • Array literal:var colors = ["red", "blue", "green"];

All array objects have property length, Method toLocaleString (), ToString (), and valueof (), where the ToString () method returns a comma-delimited string of strings that each item in the array is stitched together as a string

Some important ways to manipulate arrays

  1. Stack method
    LIFO structure, using the method push () and POP (), the push () method receives any type of argument, inserts an item from the end of the array, returns the length of the modified array, and the Pop () method returns the last item removed from the end of the array.

  2. Queue method
    FIFO structure, using the method Shift () and push (), Shift () method to remove the first item of the array and return this item; use Unshift () and pop () to simulate the queue from the opposite, the Unshift () method inserts the item from the front of the array and returns the modified array length.

  3. Reorder Methods
    Method reverse () reverses the order of the array items, the sort () method invokes the ToString () method of each array item, compares the resulting string, arranges in ascending order, and can receive a comparison function as a parameter to compare the array entry:

    function compare(value1, value2) {if (value1 < value2) {    return -1;} else if (value1 > value2) {    return 1;} else {    return 0;}}

    Another sort of comparison function in descending order:

    function compare(value1, value2) {if (value1 < value2) {    return 1;} else if (value1 > value2) {    return -1;} else {    return 0;}}
  4. Stitching, slicing, deleting, inserting, replacing methods (with concat (), slice (), splice () method)
    both the concat () and the Slice () methods create a copy of the array without affecting the original array. the Concat () method parameter is the array item to be stitched, the first parameter of the slice () method is the start position of the item to return, the second argument is the end position of the item to return (not included), and if there is only one argument, all items from the specified position to the end of the array are returned.
    Three parameters of the splice () method: The first parameter is the starting position, the second parameter is the number of items to be deleted, the third parameter is the item to be inserted, and the passed parameter can be changed, which affects the original array .

  5. Location methods (used with the indexof () and LastIndexOf () methods)
    Can have two parameters, the first parameter represents the item to be looked up, the second parameter represents the starting position of the lookup (optional), the method indexof () indicates the front-to-back, and the method LastIndexOf () indicates the forward-looking.

  6. Iterative method: Every (), filter (), ForEach (), map (), some ()
    The function that passes in the method has three parameters: the value of the array item, the position of the item in the array, and the group object itself.

  7. Merge methods: Reduce () and reduceright ()
    Reduce () iterates from the first item in the array to the last item, Reduceright () then iterates forward, summing all the items in the group with code examples:

    var values = [1, 2, 3, 4, 5];var sum = values.reduce(function(pre, cur, index, array) {return pre + cur;});alert(sum);   //15
Date type function type

Two constructor methods: function declarations and function expressions, function declarations can call functions before they are declared, and function expressions do not:

alert(sum(10, 20));                //30function sum(num1, num2) {    return num1 + num2;}
alert(sum(10, 20));                //不能正常运行var sum = function(num1, num2) {    return num1 + num2;}

JavaScript Advanced Programming 3 Learning Notes

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.