JavaScript Advanced Programming note Five (reproduced)

Source: Internet
Author: User
Tags ming prev string methods

Fifth Chapter reference types

In the previous chapter, the author mentions the concept of reference types when referring to the value of a variable. There are basic types and reference types in JavaScript, where reference types are important, and there are a lot of things we need to be aware of. From the catalog, you can see that the reference types in JavaScript are: Object type, array type, data type, regexp type, function type, basic built-in type, and monomer built-in type. Below I will tidy up the knowledge points here.

The ① reference type is a data structure used to organize data and functionality together, which is also known as a class, but JavaScript does not support the basic body of classes and interfaces, so it is called an object definition.

②object is one of the most used types. There are two methods of creating an object.

The first uses the new operator:

1 var person = new Object (), 2 person.name = "Xuchaoi"; 3 person.age = 24;

The second method uses the object literal notation:

1 var person = {2     name: ' Xuchaoi ', 3 Age     : ' 4}  //Access the value of the object: Person.name

③ creates an array and creates an object similar. Can be created by using the new operator or the array literal notation

④ detects an array by using the Array.isarray () method. Because typeof () detects that an array, object, or null type returns "Object"

⑤ splitting fractions into string methods: Join ()

1 var name = ["Xiao Ming", "Little Red", "Xiaoqing"];2 Consol.log (Name.join ("&"));  Xiao Ming & Xiao Hong & Xiao Qing

⑥ array simulation data structure stack. Push () Adds a value to the end of the array, and the pop () moves the end of the divisor group. Thus realizing the stack structure of last-in-first-out

⑦ Array simulation data structure pair. Push () Adds a value to the end of the array, and shift () shifts the first item of the divisor group. In order to realize the first-out structure

⑧unshift (), instead of shift (), adds a value to the first item of the array

⑨ Array Reverse method: reverse (). The method reverses the order of the array items.

⑩ Array Sorting method: Sort (). By default, it transforms the ToString () for each item in the array, and then arranges the arrays items in ascending order

1 var values = [0, 1, 5, ten, 15];2 Console.log (Value.sort ());    0,1,10,15,5

The result is obviously not what we want, here the sort () method takes a comparison function as an argument so that we control the order. The comparison function has two parameters, namely the previous value and the last value. If the first value is placed after the second value, a positive number is returned, whereas a negative number is returned, whichever returns 0.

function compare (value, NextValue) {    if (value < NextValue) {        return-1;    } else if (Value > NextValue) {
   return 1;    } else{        return 0;}    } var values = [1, 0, ten, 5, 15];console.log (Values.sort (Compare));    0,1,5,10,15

? Connection array: Concat (), accept parameter: string, array

The Intercept array (does not change the original array to create a new array) method: Slice (). Receive a single parameter: Start value, end value (can be omitted).

1 var colors = ["Red", "yellow", "green", "Blue"];2 var colors1 = colors.slice (1);    Intercept from the starting value to the end (the value is counted from 0) 3 var colors2 = Colors.slice (1,3)  /intercept from the starting value to the end value (excluding the end value) 4 Console.log (colors1);        ["Yellow", "green", "Blue"]  5 console.log (colors2);        ["Yellow", "green"]

? method of manipulating Arrays: splice (). The method can delete an item from an array, insert an item into an array, replace an item with an array (that is, add an item at the same time as the array item is deleted)

The location method of the array item: INDEXOF (). From the first item in the array, we look backwards at the value we set, and once we find it, we return the position index of the value in the array, and no return-1 is found. Use this to check the weight of the array

1//principle: Using indexof only returns the position of the first occurrence of the element in the array and filters the index value of the function within the filter 2 var arry = [1,2,3,4,1,2,3];3 var newarray = Arry.filter ( function (element,index,self) {4     return self.indexof (element) = = = Index;5});    Note: Filter () iterates through the array, filtering the array of elements that do not conform to the requirements of 6 console.log (NewArray);    [1,2,3,4]

? Array traversal map ().

1 var numbers = [1, 2, 3, 4, 5];2 var numbers2 = numbers.map (function (item, index, array) {3     return item * 2;4}); 5 con Sole.log (numbers2);    [2,4,6,8,10]

? Array traversal foreach ()

1 var numbers = [1, 2, 3, 4, 5];2 Numbers.foreach (function (item, index, array) {3     Array[index] = Item * 2;4}); 5 consol E.log (numbers);    [2,4,6,8,10]

? Array Cumulative Iteration method reduce ()

1 var numbers = [1, 2, 3, 4, 5];2 var sum = numbers.reduce (function (prev, cur, index, array) {3     return prev + cur;4}) ;    The Reduce iteration function accepts 4 parameters: the previous value, the current value, the index of the item, and the Array object 5 console.log (sum); 15

Here we go to the array, and the following section will continue with the note points of the reference type section!

JavaScript Advanced Programming note Five (reproduced)

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.