Web page special effects array [] {} array operation detailed
JS does not have an array of character indices except numbers
Of course! Can do that when the results are not what you think
Such as:
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
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.
return (Arr.constructor==array);
But if you use it before
Array.prototype.constructor = function () {}
Or
Arr.constructor = function () {}
There will be no more true this result
Mootools1.2dev's approach is
Array.prototype. $family = ' array '
Return (arr. $family = = ' array ');
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
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)//equal 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
var length = iterable.length, results = new Array (length);
while (length--) results[length] = Iterable[length];
return results;
MooTools's approach
Return Array.prototype.slice.call (iterable);