What is in Javascript pseudo-array? How will the pseudo-array into a standard array?

Pseudo-array (array of classes): 

Unable to directly call the desired length array method or have any special attributes behavior, does not have an array of push, pop and other methods, but they can still traverse the array traversal methods for real. Typically parameter argument function, as well as call getElementsByTagName, document.childNodes and the like, all of which belong to the dummy return NodeList object array. You can use Array.prototype.slice.call (fakeArray) array into real Array object.



log function () {

var args = Array.prototype.slice.call (arguments); // array for use unshift method, the argument into real array

args.unshift ( '(app)') ; console.log.apply (Console, args);

};



where the object is meet the following criteria called pseudo array:

1 having a length attribute

2, the data stored in index mode

3, the array does not have a push, pop or the like



, such as

1, arguments in the function .

2, by document.forms, Form.elements, Select.options, document.getElementsByName ( ),

acquired document.getElementsByTagName (), childNodes / children, etc. set (HTMLCollection, NodeList) and the like.

3, a special writing object, such as

Js collection Code Code

var obj = {};
obj [0] = "a";
obj [. 1] = "two";
obj [2] = "three";
obj.length =. 3;


they do not have some methods arrays The push, pop, shift, join the like. These dummy arrays sometimes need to turn into a real array, may be used so that push, pop or the like. The following are utility functions makeArray





Js codes


function makeArray (obj) {
var RS = [], len = obj.length;
the try {
RS = [] .slice.call (obj, 0);
} the catch (E) {// for IEs
for (var I = 0; J = obj [I ++];) {
rs.push (J);
}
}
return RS;
}





the following tests were above three dummy array:

Js Code

// define a function fun, internal makeArray convert it into an array arguments

define a function // fun, makeArray internal arguments converted into an array
function Fun () {
var = makeArray ary (arguments);
Alert (ary.constructor);
}
// Call
fun (3,5);
a plurality of passages on element p // assumed page
var document.getElementsByTagName ELS = ( "p");
var = makeArray ary1 (ELS);
Alert (ary1.constructor);
// js specific objects (e.g. jquery object)
var obj = {};
obj [0] = "a";
obj [. 1] = "two";
obj [2] = "three";
obj.length =. 3;

var ary2 makeArray = (obj);
Alert (ary2.constructor);
Pseudo-array (array of classes): 

Unable to directly call the desired length array method or have any special attributes behavior, does not have an array of push, pop and other methods, but they can still traverse the array traversal methods for real. Typically parameter argument function, as well as call getElementsByTagName, document.childNodes and the like, all of which belong to the dummy return NodeList object array. You can use Array.prototype.slice.call (fakeArray) array into real Array object.



log function () {

var args = Array.prototype.slice.call (arguments); // array for use unshift method, the argument into real array

args.unshift ( '(app)') ; console.log.apply (Console, args);

};



where the object is meet the following criteria called pseudo array:

1 having a length attribute

2, the data stored in index mode

3, the array does not have a push, pop or the like



, such as

1, arguments in the function .

2, by document.forms, Form.elements, Select.options, document.getElementsByName ( ),

acquired document.getElementsByTagName (), childNodes / children, etc. set (HTMLCollection, NodeList) and the like.

3, a special writing object, such as

Js collection Code Code

var obj = {};
obj [0] = "a";
obj [. 1] = "two";
obj [2] = "three";
obj.length =. 3;


they do not have some methods arrays The push, pop, shift, join the like. These dummy arrays sometimes need to turn into a real array, may be used so that push, pop or the like. The following are utility functions makeArray





Js codes


function makeArray (obj) {
var RS = [], len = obj.length;
the try {
RS = [] .slice.call (obj, 0);
} the catch (E) {// for IEs
for (var I = 0; J = obj [I ++];) {
rs.push (J);
}
}
return RS;
}





the following tests were above three dummy array:

Js Code

// define a function fun, internal makeArray convert it into an array arguments

define a function // fun, makeArray internal arguments converted into an array
function Fun () {
var = makeArray ary (arguments);
Alert (ary.constructor);
}
// Call
fun (3,5);
a plurality of passages on element p // assumed page
var document.getElementsByTagName ELS = ( "p");
var = makeArray ary1 (ELS);
Alert (ary1.constructor);
// js specific objects (e.g. jquery object)
var obj = {};
obj [0] = "a";
obj [. 1] = "two";
obj [2] = "three";
obj.length =. 3;

var ary2 makeArray = (obj);
Alert (ary2.constructor);

Guess you like

Origin www.cnblogs.com/superclound/p/11261506.html