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.