Home > Web Front-end > JS Tutorial > body text

js converts array-like objects into array objects_javascript skills

WBOY
Release: 2016-05-16 18:27:57
Original
1956 people have browsed it

In standard browsers, it seems that as long as the object has a length property, it can be converted into an array, but this is not the case in IE.


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

Then let’s take a look at the major Class library processing: The code is as follows:


//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;
}


jQuery The object is used to store and process DOM elements. It mainly relies on the setArray method to set and maintain the length and index. The parameter of setArray is required to be an array, so the status of makeArray is very important. This method is guaranteed to return an empty array even if there are no parameters.
$A method of Prototype.js The code is as follows:


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;
}


mootools’ $A method The code is as follows:


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’s toArray method The code is as follows:


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 || a.length);
}
}()


Ext has a more clever design and more powerful functions. It automatically executes itself from the beginning, so there is no need to judge the browser in the future. It also has two optional parameters that operate on the resulting pure array.
Finally, let’s look at dojo’s _toArray. Dojo’s implementation is always so weird. Like Ext, the last two parameters are optional, but the second one is the offset, and the last one is the existing array, used to merge the new group elements. The code is as follows:


(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;
})();
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!