Object Type: local object (1) _ basic knowledge

Source: Internet
Author: User
Object Type: local object (1) In ECMAScript, all objects are not created in the same way. Generally, three types of objects can be created and used. 3.3.1 The local object ECMA-262 defines a local object as an object provided by ECMAScript implementation independent of the host environment ". Simply put, a local object is the class defined by the ECMA-262 (reference type ). These include: you have learned about some local objects (Object, Function, String, Boolean, and Number) from the previous chapter. Some local objects will be discussed later in this book. The two important local objects to be discussed are Array and Date. 1. ArrayDifferent from Java, there is a real Array class in ECMAScript. You can create an Array object as follows: if you know the number of items in the Array in advance, you can use parameters to pass the size of the Array: use these two methods with parentheses, similar to their usage in Java: an array is created and three array items are defined: "red", "green", and "blue ". Each time an array item is added, the size of the array increases dynamically. In addition, if you know the values that should be stored in the Array, you can also declare these values with parameters to create an Array object with the same size as the number of parameters. For example, the following code creates an array with three strings: similar to a string, the first item in the array is at position 0, the second item is at position 1, and so on. You can access a specific item by using the position of the item to be read in square brackets. For example, if you want to use the array output string "green" just defined, you can use the following code: the available attribute length to get the size of the array. Like the length attribute of the string, The length attribute of the array is also the position of the last item plus 1, which means that the position of the items in the array with three items is from 0 to 2. As mentioned above, Arrays can be increased or decreased as needed. Therefore, to add an item to the previously defined array, you only need to put the value to be stored in the next unused location: in this Code, the next unused location is 3, therefore, the value "purple" will be assigned to it. Add an item to change the array size from 3 to 4. But what if I put the value in the 25 position of this array? ECMAScript will fill in the value null for all positions from 3 to 24, then place the correct value at location 25, and increase the array size to 26: an array can store up to 4294967295 items, which should meet the needs of most programming. If you want to add more items, an exception occurs. You can also define an Array object in a literal representation, that is, use square brackets ([and]) to separate values with commas. For example, you can rewrite the previous example in the following format: note that the Array class is not explicitly used in this example. Square brackets indicate that the values are stored in the Array object. The arrays declared in this way are the same as those declared in the traditional way. The Array object overwrites the toString () method and the valueOf () method, and returns a special string. This string is constructed by calling the toString () method for each item and then concatenating them with commas. For example, if you call the toString () method or valueOf () method for arrays with items "red", "green", and "blue", the return value is the string "red, green, blue ". Similarly, the toLocaleString () method returns a string consisting of array items. The only difference is that the obtained value is obtained by calling the toLocaleString () method of each array item. In many cases, the value returned by this method is the same as that returned by the toString () method, and a string is connected by a comma. Since developers may also want to create such a value outside the array, ECMAScript provides the join () method, which is used only to connect string values. The join () method has only one parameter, that is, the string used between array items. Consider the following example: here we use the join () method to create three different array representations. The first join () method uses a comma, which is essentially equivalent to calling the toString () method or the valueOf () method. The second and third join () methods use different strings and create strange separators between array items (which may not be very useful ). The point of understanding is that any string can be used as a separator. At this moment, you may want to know that, since Array has a method to convert itself into a String, does String have a method to convert itself into an Array? The answer is yes. The split () method of the String class is used here. The split () method has only one parameter. As you may have guessed, this parameter is a string that is considered as a separator between array items. Therefore, if there is a string separated by commas, you can use the following code to convert it into an Array object: If the Null String is declared as a separator, then split () each item in the array returned by the method is a string character. For example: Here, string "green" will be converted into string arrays "g", "r", "e", "e", and "n ". This function is very useful if you need to parse strings one by one. The Array object has two methods of the String class, namely the concat () and slice () methods. The concat () method processes arrays in almost the same way as it processes strings. The parameter will be appended to the end of the Array, and the returned function value is the new Array object (including the items and new items in the original Array ). For example, in this example, the concat () method is used to add the strings "yellow" and "purple" to the array. The array aColors2 contains five values, while the original array aColors still has three values. You can call the toString () method to verify the two arrays.
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.