Original: 1190000000415572
Defined:
- With the length property, other attributes (indexes) are non-negative integers (all of the objects are treated as strings, which you can understand as a non-negative integer String)
- Does not have a method that the array has
Examples of class Arrays:
var a = {' 1 ': ' gg ', ' 2 ': ' Love ', ' 4 ': ' Meimei ', length:5}; Array.prototype.join.call (a, ' + ');//' +gg+love++meimei '
Examples of Non-class arrays:
var c = {' 1 ': 2};
There is no length
attribute, so it is not an array of classes.
The common array of classes in JavaScript has the arguments
result of returning objects and Dom Methods.
For example document.getElementsByTagName()
.
Class array judgment
The JavaScript authoritative guide gives the code to determine whether an object belongs to an "array of classes." As follows:
Determine if O is an array-like object.//Strings and functions has numeric length properties, but am//excluded by The TypeOf Test. In Client-side JavaScript, DOM text//nodes has a numeric length property, and could need to being excluded//with an additio NAL o.nodetype! = 3 test.function isarraylike (o) { if (o && //o is not null, undefined, etc. typeof o = = = ' object ' && //o is an object isfinite (o.length) && //o.length is a finite numbe R o.length >= 0 && //o.length is non-negative o.length===math.floor (o.length) && O.length is an integer o.length < 4294967296) //o.length < 2^32 return true; Then O is array-like else return false; Otherwise It is not}
Class Array representation
It is a "class array" because it is similar to "array". You cannot use the array method directly, but you can use an array of classes as you would with Arrays.
var a = {' 0 ': ' a ', ' 1 ': ' b ', ' 2 ': ' C ', length:3}; An array-like objectArray.prototype.join.call (a, ' + '); = ' A+b+c ' Array.prototype.slice.call (a, 0); = = [' a ', ' b ', ' C ']: true array copyArray.prototype.map.call (a, function (x) { return x.touppercase ();}); = = [' A ', ' B ', ' C ']:
Class array objects are converted to arrays
Sometimes the best way to work with class array objects is to convert them to Arrays.
Array.prototype.slice.call (arguments)
You can then use the array method directly.
var a = {' 0 ': 1, ' 1 ': 2, ' 2 ': 3,length:3}; var arr = Array.prototype.slice.call (a); // arr=[1,2,3]
For the previous version of IE9 (the DOM implementation is com-based), we can use it makeArray
to Implement.
//Pseudo-array Conversion ArraysvarMakearray =function(obj) {if(!obj | | obj.length = = 0) { return []; } //Non-pseudo class object, directly returns the best if(!Obj.length) {returnobj; } //COM implementations for IE8 previous Dom Try { return[].slice.call (obj); } Catch(e) {vari = 0, J=obj.length, Res= []; for(; I < j; i++) {res.push (obj[i]); } returnres; }};
JavaScript class Array Object