"Reading Notes"--javascript arrays

Source: Internet
Author: User
Tags object object javascript array

An array is a linearly allocated memory that computes offsets and accesses the elements in an integer. Most languages require an array of elements to be of the same type, but a JavaScript array can contain any type.

var NULL true true }, Infinity, [' DD ', ' SS ']];  Console.log (misc.length); // Ten
Length

The length of the array is not upper bound, and if an element is saved as a subscript with a number greater than or equal to the current length, length will grow to accommodate the new element without a boundary collision.

          var myArray = [];            Console.log (myarray.length); // 0            true ;            Console.log (myarray.length); //  One

ECMAScript Standard: The subscript of an array must be an integer greater than or equal to 0 and less than 2^32-1. If you execute the following statement, the array returns the most recent length.

true ; Console.log (myarray.length); //  One

Because the result in the above code is 11, if the third line of code is removed, length is 0.

Delete
    var NULL true true }, Infinity, [' DD ', ' SS ']];     Delete misc[1];    Console.log (misc.length); // Ten    Console.log (misc[1]); // undefined

The Delete method removes only the element and does not delete the subscript.

var NULL true true }, Infinity, [' DD ', ' SS ']];            Misc.splice (  1, 1 );            Console.log (misc.length); // 9            Console.log (misc[1]); // true

The first parameter of the splice is the ordinal, and the second parameter is the number. The splice method causes the elements to be removed after the element is deleted. The ToArray method of jquery is used to convert an array.

Method

By extending a function to the prototype of an array, each array inherits this method. The following example defines a reduce method, which takes a function and an initial value as a parameter, and internally iterates through the array to invoke the method. Returns the result of the calculation. Then it is used for sum and product.

Array.prototype.reduce =function(f, value) { for(vari = 0; I < This. length; i++) {Value= f ( This[i], value); }                returnvalue;            }; vardata = [4, 5, 6, 8, 9]; varAdd =function(A, b) {returnA +b;            }; varMult =function(A, b) {returnAb;            }; Console.log (Data.reduce (Add,0));// +Console.log (Data.reduce (mult, 1));//8640
[] Defines the difference between an array and an array of {} definitions

When the array is empty, the array defined by the value undefined,{} has no length.

            varEmpty = []; varnumbers = [' 0 ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six ', ' seven ', ' eight ', ' nine ']; Console.log (empty[1]);//undefinedConsole.log (Numbers[1]);//aConsole.log (empty.length);//0Console.log (numbers.length);//Ten            varNumber_object = {' 0 ': ' 0 ', ' 1 ': ' One ', ' 2 ': ' Two ', ' 3 ': ' Three ', ' 4 ': ' Four ', ' 5 ': ' Five ', ' 6 ': ' Six ', ' 7 ': ' Seven ', ' 8 ': ' Eight ', ' 9 ': ' Nine ' }; Console.log (number_object[1]);//aConsole.log (number_object.length);//undefined

Numbers inherits Array.prototype and Number_object inherits Object.prototype so numbers inherits a lot of useful methods. But typeof numbers and typeof Number_object all returned with object.

Transformation:

The ToArray method of jquery is to invoke the slice method of the array prototype. It is primarily used to convert an element collection, a parameter to an array. Because arrays have more methods.

Slice = Array.prototype.slice;            Toarry:function() {                Slice.call (this, 0);            } 

However, the above number_object need to be converted with the Length property.

var number_object = {length:10,0: ' 0 ', 1: ' One ', ' 2 ': ' Two ', ' 3 ': ' Three ', ' 4 ': ' Four ', ' 5 ': ' Five ', ' 6 ': ' Six ', ' 7 ': ' Seven ', ' 8 ',: ' Eight ', ' 9 ': ' Nine ' }; Array.prototype.slice.call (number_object); // ["0", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]

If there is no length, or the length is not correct, an empty array is returned.

Judge:

How to tell if an object is not an array, by the above example we know that type of return is ' object ', refer to the IsArray method of jquery.

isarry:array.isarray| | function (obj) {                    return jquery.type (obj) = = = ' array ';                },                function(obj) {                     return null ? String (obj): class2type[tostring.call (obj)] | | ' object ';                }

Class2type here is a hook that contains all the types of object. And this ToString is a method of the object prototype.

  Console.log (Object.prototype.toString.call (numbers)); // [Object Array]   Console.log (Object.prototype.toString.call (number_object)); // [Object Object]

And this prototype method returns ' [Object Array] ', [object Boolean],[object data],[object function],[object number],[object regexp],[object String] several types. This makes it easy to judge.

Read the book: The Essence of JavaScript language, the insider of jquery technology

"Reading Notes"--javascript 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.