The literal notation of the JavaScript base array (010)

Source: Internet
Author: User

1. Comparison of two different methods

Arrays are objects in JavaScript, just like most other languages. We can use JavaScript's built-in array constructors to create arrays. Like the literal notation of an object, an array can also be known by literal means. And we recommend using the literal notation of the array. Here's a look at the two ways to create an array:
Array of three elements//warning:antipatternvar a = new Array ("Itsy", "Bitsy", "spider");//The exact same Arrayvar a = ["Itsy", "Bitsy", "Spider"];console.log (typeof a); "Object", because arrays is objectsconsole.log (a.constructor = = = Array); True

As in the code, the array is a set of data in square brackets, which can be any data type, including objects, when the array is known by literal fame. The literal reputation is more intuitive, more elegant, more concise, and unnecessary, and it should be created with the new Array ().

2. Traps in built-in array constructors
The reason for moving away from the built-in array constructors is not just the ones listed above. This "free" built-in constructor has many surprising questions. For example, if you only pass in an integer as an argument, it does not take the integer as the first data element, but instead uses it to set the length of the array. For example, the new Array (3) creates an array of length 3, and then if you try to read the first Data element, you will get the undefined.

An array of one elementvar a = [3];console.log (a.length); 1console.log (A[0]); 3//an array of three elementsvar a = new Array (3); Console.log (a.length); 3console.log (typeof a[0]); "Undefined"

Just passing an integer as the constructor of the built-in array is a bit disgusting but understandable, but when you pass a floating-point number as a parameter, things get worse:

Using array Literalvar a = [3.14];console.log (a[0]); 3.14var a = new Array (3.14); Rangeerror:invalid Array Lengthconsole.log (typeof a); "Undefined"

3. Determine if an object is not an array

The array uses the typeof operator, which returns "object". Although an array is an object, the result is obviously not very useful:

Console.log (typeof [1, 2]); "Object"

In ECMAScript 5 JavaScript has a whole new way of checking whether an object is data: Using Array.isarray (). It's not a good trick:

Array.isarray ([]); true//trying to fool the check//with an array-like Objectarray.isarray ({    length:1,    "0": 1,    slice:functi On () {}}); False

There is a similar approach in the EXTJS Framework: Ext.isarray (). If your code's execution environment does not support ECMAScript 5, there is no other framework, and you can use the following method to validate the array object:

if (typeof Array.isarray = = = "undefined") {    Array.isarray = function (arg) {        return Object.prototype.toString.call (arg) = = = "[Object Array]";    };}

The literal notation of the JavaScript base array (010)

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.