arrays in JavaScript [], {}, Array ()

Source: Internet
Author: User
Tags arrays constructor iterable

One, JS no other than the number of character index array
of course! Can do that when the results are not what you think
The most common is array ()
The Array object is used to store multiple values in a single variable.

To create the syntax for an Array object:

The code is as follows Copy Code
New Array ();
New Array (size);
New Array (Element0, Element1, ..., ELEMENTN);


Such as:

The code is as follows Copy Code

var p = new Array ();

P[0] = ' first value ';

P[1] = ' second value ';

p[' h '] = ' can access this value ';

The above can be almost php like, but p.length = 2;
To cycle through the values in the array, you can only
for (var key in P) {

}

This is a serious problem.
Array has a prototype property that can extend the method of the Array object
If this

The code is as follows Copy Code
Array.prototype.next = function () {};

All array objects will have one more index value
Next:function () {};
The loop above will also print one more of your unexpected values.
So, the specification of array is close to the array in C language,


second, [] is the new Array ()
It makes it easy to create multidimensional arrays


three, {} is new Object ()
actually this is closest to the array in PHP
It can be said that the arrays in PHP are set hash array in one


judge whether it is an array
Maybe you'll think of TypeOf.
But regardless of who created the object above
Apply typeof arr= ' object '
Should be judged like this.

The code is as follows Copy Code

return (Arr.constructor==array);

But if you use it before
Array.prototype.constructor = function () {}

Or
Arr.constructor = function () {}


Still can't be completely convinced

Five, arguments is not an array
Arguments is the currently executing function object that returns a arguments object.
Function.arguments
The function argument is the name of the currently executing function and can be omitted.
Arguments contains some subordinate properties Arguments.callee, Arguments.length
Describe them in two pieces of code

  code is as follows copy code

  function param () {

     var s= ';

     for (var i=0,l=arguments.length;i<l;i++) {

         s + + ' param ' + i+ ': ' + arguments[i] + ' n ';

    }

     alert (s);

  }

   param (1,2,3);

Function factorial (n) {

  if (n <= 0)

  return 1;

  Else

    return n * Arguments.callee (N-1)//is equivalent to factorial (n-1)

}

Alert (factorial (3));


You'll think it's not an array if you read it! No

Arguments[[0|1|2|...| is not traversed in the form of a for (var k in arguments) N
But there are some places that need this feature, and I have to convert arguments to array.
Prototype.js's approach

The code is as follows Copy Code

var length = iterable.length, results = new Array (length);

while (length--) results[length] = Iterable[length];

return results;

This summarizes a number of uses for JavaScript arrays, as well as various uses of [], {}, Array () in the array.

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.