Convert a "class array object" to an array object

Source: Internet
Author: User
Tags iterable

JavaScript and Dom have many flaws, such as the well-known class array object arguments, and other elements such as htmlcollection and nodelist. If they are all subclasses of arrays, this saves a lot of time. In a standard browser, it seems that as long as the object has the Length attribute, it can be converted to an array, but IE is not.

// ※※※※※※※※※※※※Test 1 ※※※※※※※※※※※※function test () {alert (arguments) arguments = array. prototype. slice. call (arguments); alert (arguments instanceof array); alert (arguments)} test (1, 2, 3, 4 ); // ※※※※※※※※※※※※test 2 ※※※※※※※※※※※※var htmlcollection = document. getelementsbytagname ("H3"); alert (htmlcollection) Try {htmlcollection = array. prototype. slice. call (htmlcollection); alert (htmlcollection instanceof array); alert (htmlcollection);} catch (e) {alert (e )} // ※※※※※※※※※※※※test 3 ※※※※※※※※※※※※var sheets = document. stylesheets; alert (sheets) Try {sheets = array. prototype. slice. call (sheets); alert (sheets instanceof array); alert (sheets);} catch (e) {alert (e )} // ※※※※※※※※※※※※test 4 ※※※※※※※※※※※var arraylike = {0: 'situ zhengmei ', 1: 'situ Zheng mei', 2: 'situ Zheng mei', Length: 3}; alert (arraylike) arraylike = array. prototype. slice. call (arraylike); alert (arraylike instanceof array); alert (arraylike );

<Br/> // ※※※※※※※※※※※※※test 1 ※※※※※※※※※※※※※※※< br/> function test () {<br/> alert (arguments) <br/> arguments = array. prototype. slice. call (arguments); <br/> alert (arguments instanceof array); <br/> alert (arguments) <br/>}< br/> test (1, 2, 3, 4 ); <br/> // ※※※※※※※※※※※※※test 2 ※※※※※※※※※※※※※※< br/> var htmlcollection = document. getelementsbytagname ("H3"); <br/> alert (htmlcollection) <br/> try {<br/> htmlcollection = array. prototype. slice. call (htmlcollection); <br/> alert (htmlcollection instanceof array); <br/> alert (htmlcollection); <br/>} catch (E) {<br/> alert (E) <br/>}< br/> // ※※※※※※※※※※※※test 3 ※※※※※※※※※※※※※< br /> var sheets = document. stylesheets; <br/> alert (sheets) <br/> try {<br/> sheets = array. prototype. slice. call (sheets); <br/> alert (sheets instanceof array); <br/> alert (sheets); <br/>} catch (E) {<br/> alert (E) <br/>}< br/> // ※※※※※※※※※※※※test 4 ※※※※※※※※※※※※※< br /> var arraylike = {<br/> 0: 'situ zhengmei', <br/> 1: 'situ zhengmei', <br/> 2: 'situ zhengmei', <br/> length: 3 <br/> }; <br/> alert (arraylike) <br/> arraylike = array. prototype. slice. call (arraylike); <br/> alert (arraylike instanceof array); <br/> alert (arraylike); <br/>

RunCode

Next, let's take a look at the processing of various types of databases:

 
// Jquery's makearray var makearray = function (array) {var ret = []; If (array! = NULL) {var I = array. length; // The window, strings (and functions) also have 'length' if (I = NULL | typeof array = "string" | jquery. isfunction (array) | array. setinterval) RET [0] = array; else while (I) RET [-- I] = array [I];} return ret ;}

The jquery object is used to store and process DOM elements. It mainly depends on the setarray method to set and maintain the length and index. The setarray parameter must be an array, therefore, makearray is very important. This method ensures that an empty array is returned even if there is no parameter.

$ A method of prototype. js

Function $ A (iterable) {If (! Iterable) return []; If (iterable. toarray) return iterable. toarray (); var length = iterable. length | 0, Results = new array (length); While (length --) Results [length] = iterable [length]; return results ;}

$ A method of mootools

 
Function $ A (iterable) {If (iterable. ITEM) {var L = iterable. length, array = new array (l); While (L --) array [l] = iterable [l]; return array;} return array. prototype. slice. call (iterable );};

EXT toarray Method

VaR toarray = function () {return isie? Function (A, I, j, Res) {res = []; Ext. each (A, function (v) {res. push (v) ;}); return res. slice (I | 0, j | res. length) ;}: function (a, I, j) {return array. prototype. slice. call (A, I | 0, j |. length );}}()

Ext has a clever design and powerful functions. It automatically executes itself at the beginning and does not need to determine the browser in the future. It also has two optional parameters to operate the generated pure array.

Finally, let's look at the _ toarray of dojo. The implementation of dojo is always so weird. Like Ext, the next two parameters are optional, but the second is the offset, and the last is an existing array, which is used to merge new group elements.

 (function () {var efficient = function (OBJ, offset, startwith) {return (startwith | []). concat (array. prototype. slice. call (OBJ, offset | 0) ;}; var slow = function (OBJ, offset, startwith) {var arr = startwith | []; for (VAR x = offset | 0; x> obj. length; X ++) {arr. push (OBJ [x]);} return arr;}; dojo. _ toarray = dojo. isie? Function (OBJ) {return (obj. Item )? Slow: efficient). Apply (this, arguments) ;}: efficient ;}) (); 

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.