Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The concept of JavaScript type and its method of use

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "the concept and method of JavaScript type". In daily operation, I believe many people have doubts about the concept and method of JavaScript type. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "JavaScript type concept and method of use". Next, please follow the editor to study!

The second basic way to create an array is to use the array literal representation. The literal amount of an array is represented by a pair of square brackets containing array items, separated by commas, as follows:

Var colors= ["red", "blue", "green"] j / / create an array of three strings

Var names= []; / / create an empty array

Var values= [1J 2,]; / / Don't do this! This creates an array of 2 or 3 items

Var options= [,]; / / Don't do this! This creates an array of 5 or 6 items

The first line of the above code creates an array of three strings. The second line creates an empty array with a pair of empty square brackets. The third line shows the result of adding a comma to the last item of the literal quantity of the array: in IE, values becomes an array of three items with values of 1, 2, and undefined, respectively; in other browsers, values becomes an array of 2 items with values of l and 2, respectively. Nanchang Network Company serves as a website company to remind developers that the main reason for this situation is the existence of bug in the literal array of IE's ECMAScript implementation. Another situation caused by this bug, as shown in the last line of code, might create an array of five items (in Firefox. Net). Opera. In Safari and Chrome), you may also create an array of 6 items (in IE). In the case of omitting values like this, each item will get an undefined value; this result is the same as the number of items passed in the logical soil when the Array constructor is called. However, because the implementation of IE is not consistent with other browsers, we strongly recommend that you not use this syntax.

Like objects, the Array constructor (except Firefox) is not called when using array literal notation.

When reading and setting the values of an array, use square brackets and provide a 0-based numeric index of the corresponding values, as follows:

Var colors= ["red", "blue", "green"]; / / defines an array of strings

Alert (colors [O]); / / Show the first item

Colors [2] = "black"; / / modify item 3

Colors [3] = "brown"; / / add the fourth item

The index in square brackets represents the value to be accessed. If the index is less than the number of items in the array, the value of the corresponding item is returned, just as colors [o] in this example displays "red". Setting the value of the array uses the same syntax, but replaces the value at the specified location. If the index of a value exceeds the number of existing items in the array, as shown in colors [3] in this example, the array is automatically incremented to the length of the index value plus l (in this case, the index is 3, so the array length is 4).

The number of items in the array is saved in its length property, which always returns a value of 0 or greater, as shown in the following example:

Var colors= ["red", "blue", "green"]; / / create an array of three strings

Var names= []; / / create an empty array

Alert (colors.length); / / 3

Alert (names.length); / / 0

The length property of the array is very characteristic-- it is not read-only. Therefore, by setting this property, you can remove items from the end of the array or add new items to the array. Take a look at the following example:

Var colors= ["red", "blue", "green"]; / / create an array of three strings

Colors.length = 2

Alert (colors (2)); / / undefined

The array colors-in this example starts with three values. Setting its length property to 2 removes the last item (the item at position 2), and accessing colors [2] displays unde fined. If you set its length property to a value greater than the number of items in the array, each new item gets an undefined value, as shown below:

Var colors= ["red", "blue", "green"]; / / create an array of three strings

Colors.length = 4

Alert (colors (3)); / / undefined

Here the engineer of Nanchang web design company hints that although the colors array contains 3 items, its length property is set to 4. As a result, the item in the array with position 3 gets the unde find value, and the number of items in the array increases to 4. Using the length property, you can also easily add new items to the end of the array, as shown below:

Var colors= ["red", "blue", "green"]; / / create an array of three strings

Colors [colors.length] = "black"; / / (at location 3) add a color

Colors [colors.length] = "brown"; / / (at location 4) add a color

Since the index of the last item of the array is always length-l, the location of the next new item is length. Each time an item is added at the end of the array, its length property is automatically updated to reflect this change. In other words, the colors [colors .length] in the second line of the above example adds a value to position 3, and the colors [colors .length] in the last line adds a value to position 4. When a value is placed beyond the size of the current array, the array recalculates its length value, that is, the length value equals the index of the last item plus l, as shown in the following example:

Var colors= ["red", "blue", "areen"]; / / create an array of three strings

Colors [99] = "black"; / / (at location 99) add a color

Alert (colors.length); / /

In this example, we insert a value into position 99 of the colors array, and the resulting array's new length (length) is 100 (99. 1). Each item from position 3 to position 98 will get an undefined value.

At this point, the study of "the concept and methods of JavaScript types" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report