Array of objects for JavaScript framework design

Source: Internet
Author: User
Tags setinterval mootools

Class Array object is a good storage structure, but the function is too weak, in order to enjoy the pure array of what convenient method, before using can do the next conversion, usually can use the $.slice.call () method to do the conversion, but the old version of IE Htmlcollection, NodeList is not a subclass of object, and if using the [].slice.call () method may result in an exception, here's how the large libraries are handled:

1, the Makearray of jquery

<!    DOCTYPE html>//It is generally necessary to convert an object into an array using the [].slice.call () method, but in the old version of IE Htmlcollection, nodelist is not a subclass of object, it is a COM object        //It is not possible to use the [].slice.call () method to get an array of incoming objects, the following is an array method of jquery compatible with old versions of IE        //This method has the following guarantees        /*1, regardless of whether the parameter is passed in, always return an array, if not, return an empty array 2, the parameters passed in (does not contain the length property, is a string, is the jquery method, is an array of setinterval) their reference to the array The first item 3, if the passed parameters conform to the requirements of the array, then the array*/        //Note: The incoming collection must have a length property, and then the collection's key value must be a number, that is, a collection with an array structure, to be converted        varmakearray=function(array) {varret=[]; if(array!=NULL)            {                varL=Array.Length; if(l==NULL||typeofarray=== "string" | | Jquery.isfunction (array) | |array.setinterval) {ret[0]=Array; }                Else                {                     while(l) ret[--l]=Array[l]; }            }            returnret; } alert (Makearray ({length:3,0: "A", 1: "B", 2: "C"}) [1]); </script></body>

2. Method of object array of Dojo

<! DOCTYPE html>/*Dojo's Object array method is the same as EXT, all at the beginning of the browser type, he also has two parameters, used to manipulate the converted array But after the two parameters of dojo, not to intercept the array's start index and end Index Dojo's first parameter The number is the object to be converted to an array, the second is the offset, the third is an existing one, the return value is an existing array, and the converted and truncated merged array*/        varZc={}; Isie=true; (function(){            varEfficient=function(obj,offest,startwith) {return(startwith| | []). Concat ([].slice.call] (Obj,offest | | 0)); }            varslow=function(obj,offest,startwith) {varArr=startwith | | []; //offset does not support negative number                  for(varI=offest | | 0;i<obj.length;i++) {Arr.push (obj[i]); }                returnarr; } Zc.toarray=isie?function(obj) {returnSlow.apply ( This, arguments);        }:efficient;        })(); Alert (Zc.toarray ({length:3,0: "A", 1: "B", 2: "C"},0,[1,2,3])); </script></body>

3, the object array of Ext method

<! DOCTYPE html>/*ext Design is clever, in the framework of a load at the same time, to determine the type of browser, and then stored in the variable, then do not need to judge Browse, and then according to the browser is not ie to choose toarray exactly refers to the method body, if it is IE browser, then hanging with a custom Like an array method, if not call [].slice.call (), and through the slice method, through the I,j parameter to the string interception operation*/        /*This method has the following guarantee 1, if executed under IE browser, then call the custom object array Method 2, if no longer ie, hanging with [].slice.call () for Object array 3, can provide two parameters (STA Rt,end), which is used to intercept the converted object array of the specified length*/        vartoarray=function () {            varReturnisie;//determine if the browser is IE            returnReturnisie?function(a,i,j) {varLength=a.length | | 0,result=NewArray (length);  while(length--) Result[length]=A[length]; returnResult.slice (I | | | 0,j| |a.length); }:function(a,i,j) {returnArray.prototype.slice.call (a,i | | 0,j | |a.length);        };        }(); varRes=toarray ({length:2,0: "A", 1: "2"},1); Alert (RES)</script></body>

4, MooTools

<! DOCTYPE html>/*MooTools method of object array*/    /*This method has the following guarantee 1, when the user passed in is the Htmlcollection collection is, because the old version of IE HTML node object is a COM object, not a subclass of the JS object, so the [].slice.call () method can not use the custom object array method 2. If the incoming object is not the case above, then the [].slice.call () method is used for array of objects*/    function$A (array) {if(array.item) {varLength=array.length | | 0,result=NewArray (length);  while(length--) Result[length]=Array[length]; returnresult; }        returnArray.prototype.slice.call (Array); }    varRes= $A ({length:2,0:1,1:2});//Output:alert (RES)</script></body>

5, Prototype

<! DOCTYPE html>/*prototype the method of converting an array of objects*/    /*This method has the following guarantee 1, if the parameter is not passed in, return an empty array 2, if the current browser supports the ToArray () method, then call the object's ToArray () Method 3, if the above two conditions are not satisfied, then get the current object's Length property (if there is no Give 0), and then new an array with length lengths and assign the values*/    //Note: The length of an object to be converted to an array must not be greater than the actual element or the length of the actual element    function$A (array) {if(!array)return []; if(Array.toarray)returnArray.toarray (); varLength=array.length | | 0,results=NewArray (length);  while(length--) Results[length]=Array[length]; returnresults; }    varresult= $A ({length:3,0:1,1:2,2:3}); alert (result);</script></body>

6, Mass

<! DOCTYPE html>/*here is an array of objects for the mass method*//*This method has the following guarantees: 1, the first to distinguish the browser 2, if it is IE, call the custom array method, if not, then use [].slice.call3, provide the start and end parameters, convenient to (the array after the object array) to intercept 4, Ensure that the input of the start and end parameters does not affect the output*/Isie=true;varToarray=window.isie?function(nodes,start,end) {varRet=[],length=nodes.length; if(end===void0 | |typeofend=== "Number" &&isfinite (end)) {Start=parseint (start,10) | | 0; End=end==void0?length:parseint (end,10); if(start<0) Start+=length; if(end>length) End=length; if(end<0) End+=length;  for(vari=start;i<end;i++) {Ret[i-start]=Nodes[i]; }    }    returnret;}:function(nodes,start,end) {returnArray.prototype.slice.call (nodes,start,end);};varRes=toarray ({length:3,0:1,1: "A", 2: "B"},0,-1);//Output: 1,aalert (res);</script></body>

Array of objects for JavaScript framework design

Related Article

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.