Type of object: Local Object (1) _ Basics

Source: Internet
Author: User
In ECMAScript, all objects are not created equally. Generally, there are three of objects that can be created and used.

3.3.1 Local Object

ECMA-262 defines the local object (native object) as "the object provided by the ECMAScript implementation that is independent of the host environment." In short, the local object is the ECMA-262-defined class (reference type). They include:
You've learned some local objects (object, Function, String, Boolean, and number) from the previous chapter, and some local objects are also discussed in the chapters later in this book. The two important local objects to be discussed now are array and date.
1. Array Class
Unlike Java, there is a true array class in ECMAScript. You can create an array object as follows:
If you know the number of items in an array in advance, you can pass the size of the array with parameters:
Using these two methods, you use parentheses in a way that is similar to their usage in Java:
This creates an array and defines three array entries, namely "Red," "Green," and "Blue." With each additional array item, the size of the array grows dynamically.
In addition, if you know the values that the array should hold, you can also declare these values with parameters, creating an array object of equal size and parameter numbers. For example, the following code creates an array with three strings:
Like 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 in square brackets where the item you want to read is placed. For example, to use the array output string "green" that you just defined, you could use the following code:
The available attribute length gets the size of the array. As with the length property of the string, the length property of the array is also the position of the last item plus 1, meaning that items in an array with three items are positioned from 0 to 2.
As mentioned earlier, arrays can grow or decrease as needed. So, if you want to add an item to the array you defined earlier, simply put the value you want to store in the next unused location:
In this code, the next unused position is 3, so the value "purple" will be assigned to it. Add one to make the size of the array change from 3 to 4. But what if you put the value in the position of this array 25? ECMAScript will fill in the value null for all positions from 3 to 24, then place the correct value at position 25 and increase the size of the array to 26:
Arrays can hold up to 4294967295 items, which should meet the needs of most programming. If you want to add more items, an exception will occur.
You can also define an array object in literal notation, that is, by using square brackets ([and]) and by separating values with commas. For example, you can rewrite the previous example in the following form:
Note that in this example, the array class is not explicitly used. The square brackets imply that the values are stored in the array object. An array declared in this manner is the same as an array declared in a traditional manner.
The array object overrides the ToString () method and the ValueOf () method, returning a special string. The string is formed by invoking the ToString () method on each item and then connecting them together with commas. For example, you call the ToString () method or the ValueOf () method for an array with item "Red", "green", and "blue", and the string "Red,green,blue" is returned.
Similarly, the toLocaleString () method returns a string consisting of an array item. The only difference is that the resulting value is obtained by invoking the toLocaleString () method of each array item. In many cases, the value returned by the method is the same as the value returned by the ToString () method, and the string is concatenated with a comma.
Since the developer may also want to create such a value outside the array, ECMAScript provides a method join (), and its only use is the connection string value. The join () method has only one parameter, the string used between the array items. Consider the following example:
Here, a method join () is used to create three different array representations. The first join () method uses commas, essentially equivalent to calling the ToString () method or the ValueOf () method. The second and third join () methods use different strings, creating strange delimiters (which may not be useful) between the array entries. The point of understanding is that any string can be used as a separator.
Now maybe you want to know, since array has a way of converting itself into strings, does string have a way of converting itself into groups? The answer is yes. The method split () of the string class is being used for this. The split () method has only one parameter. As the reader might have guessed, the argument is a string that is considered a delimiter between array items. So, if you have a comma-delimited string, you can convert it to an array object using the following code:
If an empty string is declared as a delimiter, each item in the array returned by the split () method is a string character, for example:
Here, the string "green" is converted to the string array "G", "R", "E", "E" and "N". This is useful if you need to parse strings by character.
The array object has two methods, namely the concat () and the Slice () method, that the string class has. The Concat () method handles arrays almost exactly the way it handles strings. The parameter is appended to the end of the array, and the returned function value is the new array object, including the items in the original array and the new item. For example:
In this example, the string "yellow" and "purple" are added to the array using the Concat () method. The array aColors2 includes 5 values, while the original array acolors still has only 3 values. This can be proved by calling the ToString () method separately for two arrays.

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.