Home  >  Article  >  Web Front-end  >  Detailed code examples of how the javascript class library converts class objects into array objects

Detailed code examples of how the javascript class library converts class objects into array objects

伊谢尔伦
伊谢尔伦Original
2017-07-26 11:16:591331browse

Let’s take a look at the processing of various major libraries:

//jQuery的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 relies on the setArray method to set and maintain the length and Index, and 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

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

ToArray method of Ext

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’s design is more clever and its function is more powerful . 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, look at dojo's _toArray. The implementation of dojo 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.

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


The above is the detailed content of Detailed code examples of how the javascript class library converts class objects into array objects. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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