JavaScript data Types Learning notes sharing _javascript skills

Source: Internet
Author: User
Tags array length prev

This article examples for you to explain the JavaScript data types of relevant information for your reference, the specific contents are as follows

1. Reference type
a value of a reference type is an instance of a reference type, which is a data structure used to organize data and functionality, and is often called a class.
Object is an instance of a specific reference type that is created using the new operator followed by a constructor, which is itself a function and is defined for the purpose of creating a new object.

var person = new Object ();

2.Object type
(1) object type is an ideal choice for storing and transferring data in an application.
(2) Object Type instance Creation method:
Use the new Plus object constructor.

 <script type= "Text/javascript" >
      var person = new Object ();
      Person.name = "Peter";
      alert (person.name);
 </script>

Use the representation of the literal amount of the object.

var dog = {
        name: "Kity",
        age:3,
        eye:bigeyes
      };

This creation method starts with a pair of curly braces, preceded by a variable name, followed by a colon, a property value, and can have multiple properties, but each attribute must be separated by commas, and the last attribute is generally not comma-delimited. Where the property name can also be a string. If you don't write in curly braces, you can only define objects that contain default properties and methods.

In general, access to object properties uses the dot notation, which uses square brackets [] to access the properties of the object in JavaScript. When you use [], the property that you want to access should be in the form of a string.

Alert (person["name"]);
alert (person.name);

The former can access the property through a variable.
If the property name contains a character that causes a syntax error, the property name uses a keyword or a reserved word, or you can use square brackets.
Unless you must use a variable to access the property, we recommend that you use a dot notation.

3.Array type
An array in JavaScript is a sequence table that can hold data of any type, which is the largest difference from an array of other languages, and its array size can be dynamically adjusted.
(1) Basic way to create an array:
You can use the array constructor to pass values (the size of an array or the contents of an array)

 var student = new Array ();
  var student = new Array (a);
  var student = new Array ("Peter", "Merry", "Bob");

(2) using an array literal representation method, the array literal is represented by a pair of square brackets that contain the array items, separated by commas between multiple arrays.
(3) When reading and setting the value of an array, the square brackets and corresponding values are based on a numeric index of 0.
The length of the array is stored in the value of the length property, which can return 0 or greater values. You can delete or add new items from the end of the array by setting the value of the length property.

 var colors = ["Red", "Blue", "green"];
  Colors.length = 2;//array length becomes 2,green removed
  alert (colors[2);//At this point the access returns to Undefined,alert (COLORS[1); returns blue

If you set the Length property to be greater than the value of the array item, each new item returns the undefined value.

 var colors = ["Red", "Blue", "green"];
  Colors.length = 5;
  Alert (colors[4]);

Use the Length property to add an entry at the end of the array.

 var colors = ["Red", "Blue", "green"];
  Alert (colors[colors.length]= "Black");

(4) array can contain up to 4,294,967,295 items
(5) Detection array
Using the Array.isarray () method, it can ultimately determine whether the value is an array, and regardless of the global execution environment in which he was created.

 if (Array.isarray) {
    //operates 
  }

(6) Conversion method
All objects have tolocalestring (), toString (), and valueof () methods, where the ToString () method of the calling array returns a comma-delimited string of each string in the array that is worthy of concatenation. ValueOf () returns an array.
When the toLocaleString () method is invoked, an array is created that is worth a comma-delimited string, unlike the previous one, in order to get the value of each item, the toLocaleString () method for each item is invoked, not the ToString () method.

<script type= "Text/javascript" >
      var person1 = {
        tostring:function () {return
          "Peter";
        },
        Tolocalestring:function () {return
          "Mary";
        }
      };
      var Person2 = {
        tostring:function () {return
          "n";
        },
        tolocalestring:function () {return
          "18 ";
        }
      };
      var person = [Person1,person2];
      alert (person);
      Alert (person.tostring ());
      Alert (person.tolocalestring ());
    </script>

Use the Join () method to build this string with a different delimiter, the join () method receives only one argument, serves as a string for the delimiter, and then returns a string containing all the array entries. If you do not pass in any values to the join () method, or if you pass in undefined, use commas as delimiters. If the value of an item in an array is null or undefined, the value is represented in an empty string in the result returned by the join (), toString (), tolocalestring (), and the ValueOf () method.

<script type= "Text/javascript" >
      var person1 = {
        tostring:function () {return
          "Peter";
        },
        Tolocalestring:function () {return
          "Mary";
        }
      };
      var Person2 = {
        tostring:function () {return
          "n";
        },
        tolocalestring:function () {return
          "18 ";
        }
      };
      var person = [Person1,person2];
      alert (person);
      Alert (person.tostring ());
      Alert (person.tolocalestring ());
      Alert (Person.join ("*"));
      Alert (Person.join ("--)");
    </script>

(7) Stack method
The stack is the data structure of the stack, his characteristics are advanced and out, and all the operation only occurs on the top of the stack, JavaScript provides a push () and Pop () method, can achieve similar to the stack behavior.
Push () can receive any number of parameters and is added to the end of the array one by one and returns the length of the modified array.
The Pop () method removes the last item from the end of the array, reduces the lengthh value of the array, and then returns the top of the removal.
(8) Queue method
The data structure of the queue is advanced first out, the queue adds items at the end of the list, and the item is removed from the front of the list.
Push () can add an item to the end of the array, shift () to move the first item in the divisor group and return the item, the array length minus 1;unshift () can add any items to the front of the array and return the length of the new array.
(9) Reordering methods
Reverse () Reverses the order of the arrays
By default, sort () arranges the array items in ascending order (the minimum is at the top, the maximum is on the last side, and for sorting, the sort () method invokes the ToString () transformation method for each array item and then compares the resulting string to determine how to sort). Sort () can receive a comparison function as an argument, and the comparison function receives two arguments, returns a negative number if the first argument should precede the second, and returns 0 if the two arguments are equal, and returns a positive number if the first argument should be in the second.
The return values of the reverse () and sort () methods are all sorted arrays.
(10) Operation method
Concat () can create a new array based on all the items in the current array.
Slice () can create a new array based on one or more items in the current array, and slice () can receive one or two arguments, returning the starting and ending positions of the items. When the parameter is one, it returns all the items at the beginning of the parameter at the end of the current array, or an entry between the start and end positions, if there are two parameters, but not the ending position.
Splice () method: Primarily used to insert items into the middle of an array
Delete: You can delete any number of items by specifying only 2 parameters, the position of the first item to delete, and the number of items to delete.
Insert: You can insert any number of items to a specified location by providing only three parameters, starting position and 0, and the item to insert.
Replace: You can insert any number of items to a specified location and delete as many items as you want, specifying 3 parameters, starting positions, the number of items to delete, and any number of items to insert, which do not have to be equal to the number of items deleted.
Splice () always returns an array that contains items that are deleted from the original array (an empty array is returned if no items are deleted).
(11) Location method
Both of these methods can receive two parameters, the items to look for, and the index that represents the start position of the lookup. Returns the position of the item to find in the array, or returns 1 if it is not found, using the strict equality operator when comparing the first argument with each item in the array, and the item to be looked for must be strictly equal.
IndexOf (): Searching backwards from the beginning of an array
LastIndexOf (): Start looking forward from the end of the array.
(12) Iterative method
JavaScript provides 5 iterative methods for an array, each method receives two parameters: the function to run on each item and (optionally) the scope object (the value that affects this), and the functions passed into these methods receive three parameters: the value of the array item, the position in the array, and the group object itself.
Every (): Runs the given function for each item in the array, and returns True if the function returns true for each item.
Filter (): Each item in an array runs the given function, which returns an array of items that return the function to True
ForEach (): Run the given function for each item in the array, this method has no return value
Map (): Each item in an array runs the given function, returning an array of the results of each function call
Some (): Runs the given function for each item in the array, and returns True if the function returns true for either item.
None of the above methods modifies the containing value in the array.
Every () and sum () are used to query whether an item in the array satisfies a condition
(13) Reduction method
The following two methods will iterate over all the items of the group, and then build a final return value. Receives two parameters: an initial value that invokes the function and (optionally) as the base of the reduction on each item. The two methods are passed to receive 4 parameters: the previous value, the current value, the index of the item, and the array object. Any value returned by this function is automatically passed to the next item as the first argument, and the first iteration takes place on the second item of the array, so the first argument is the first item of the array, and the second is the second item of the array.
Reduce (): begins with the first item of an array, traversing to the last. You can perform all the worthwhile operations in the array.

<script type= "Text/javascript" >
      var values=[1,2,3,4,5];
      var sum = values.reduce (function (prev,cur,index,array) {return
        prev + cur;
      });
      alert (sum);
  </script>

Reduceright (): Begins with the last item in the array and traverses forward to the first item. You can perform all the worthwhile operations in the array.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.