$A方法: Accepts an array-like collection (anything with numeric indices) and returns its equivalent as an actual Array object. This method is a convenience alias of Array.from, but is the preferred way of casting to an Array.
function $A(iterable) { if (!iterable) return []; if ('toArray' in Object(iterable)) return iterable.toArray(); var length = iterable.length || 0, results = new Array(length); while (length--) results[length] = iterable[length]; return results; }
如果傳進(jìn)來的參數(shù)為null, undefined and false 則直接返回空數(shù)組 如果傳進(jìn)來的參數(shù)對(duì)象里有toArray方法,這直接調(diào)用參數(shù)的toArray方法,因?yàn)橛性S多Prototype對(duì)象里面已經(jīng)定義好了toArray方法,所以可以直接調(diào)用toArray方法 例如:
/*The well-known DOM method document.getElementsByTagName() doesn't return an Array, but a NodeList object that implements the basic array "interface." Internet Explorer does not allow us to extend Enumerable onto NodeList.prototype, so instead we cast the returned NodeList to an Array:*/ var paras = $A(document.getElementsByTagName('p')); paras.each(Element.hide); $(paras.last()).show();