[转帖]Mootools源码分析-04 -- Array
生活随笔
收集整理的這篇文章主要介紹了
[转帖]Mootools源码分析-04 -- Array
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//對數組的擴展實現
Array.implement({
//迭代方法,call的使用
forEach: function(fn, bind) {
for (var i =0, l =this.length; i < l; i++) fn.call(bind, this[i], i, this);
}
});
//將each作為forEach的別名
Array.alias('forEach', 'each');
//轉為數組的快捷方式,但是在IE下,對于XML對象使用XPath查詢后的結果,$A方法無法達到預期的結果
function $A(iterable) {
if (iterable.item) { //對于collection,使用for遍歷復制到數組
var array = [];
for (var i =0, l = iterable.length; i < l; i++) array[i] = iterable[i];
return array;
}
//對于arguments和array對象,使用本方法可以轉為數組
return Array.prototype.slice.call(iterable);
};
//通用迭代遍歷方法
function $each(iterable, fn, bind) {
var type = $type(iterable);
((type =='arguments'|| type =='collection'|| type =='array') ? Array : Hash).each(iterable, fn, bind);
};
/*
對Array的擴展實現
對于迭代遍歷時,給fn傳遞的參數依次為:當前數組項,當前項的索引值,當前數組對象,比如:
7前逢單7后雙,判斷月份大小的算法
function myfilter(item, i, arr){
var half = arr.length / 2 + 1;
return half > i && item % 2 || half <= i && !(item % 2);
}
[1,2,3,4,5,6,7,8,9,10,11,12].filter(myfilter);
上面的代碼將返回[1, 3, 5, 7, 8, 10, 12]
然而,很多時候我們只需要使用第一個參數,即當前項的值
*/
Array.implement({
//對數組的每一項使用一個邏輯判斷,僅當所有項通過邏輯判斷時返回true
every: function(fn, bind) {
for (var i =0, l =this.length; i < l; i++) {
if (!fn.call(bind, this[i], i, this)) returnfalse;
}
returntrue;
},
//對數組進行邏輯過濾,返回包含所有通過邏輯判斷的項的數組
filter: function(fn, bind) {
var results = [];
for (var i =0, l =this.length; i < l; i++) {
if (fn.call(bind, this[i], i, this)) results.push(this[i]);
}
return results;
},
//配合$defined和filter方法,清除數組中的空項/
clean: function() {
returnthis.filter($defined);
},
/*
類似String對象的indexOf方法,返回數組在指定位置開始查找指定的匹配項,并返回其索引值,沒有找到時返回-1
from參數可以在0和數組長度-1之間取值,也可以為負值,當為負值時表示開始查找的位置從后往前的位數
*/
indexOf: function(item, from) {
var len =this.length;
for (var i = (from <0) ? Math.max(0, len + from) : from ||0; i < len; i++) {
if (this[i] === item) return i; //注意這里用的是絕對等于===比較
}
return-1;
},
//對數組中的每項進行處理,fn函數的返回值將代表原位置的項,最后返回一個經處理后的新數組
map: function(fn, bind) {
var results = [];
for (var i =0, l =this.length; i < l; i++) results[i] = fn.call(bind, this[i], i, this);
return results;
},
//與every方法相反,只需要數組中的有一項滿足條件就返回true
some: function(fn, bind) {
for (var i =0, l =this.length; i < l; i++) {
if (fn.call(bind, this[i], i, this)) returntrue;
}
returnfalse;
},
//將兩個數組關聯,組成鍵-值表示的對象,注意數組充當鍵還是值的決定因素--當前數組的每項作為值
associate: function(keys ){
var ōbj = {}, length = Math.min(this.length, keys.length);
for (var i =0; i < length; i++) obj[keys[i]] =this[i];
return obj;
},
//將數組的每項使用參數對象的方法進行處理,當返回true時當前項被添加到鍵值對象中作為值,處理的方法名作為鍵
link: function(object) {
var result = {};
for (var i =0, l =this.length; i < l; i++) {
for (var key in object) {
if (object[key](this[i])) {
result[key] =this[i]; //鍵和值的關系
delete object[key]; //保證result中鍵不重復,數組中索引值越小優先級越高
break;
}
}
}
return result;
},
//利用index方法判斷數組中是否包含指定項
contains: function(item, from) {
returnthis.indexOf(item, from) !=-1;
},
//擴展數組,將指定數組加到當前數組后面
extend: function(array){
for (var i =0, j = array.length; i < j; i++) this.push(array[i]);
returnthis;
},
//獲取數組的最后一項,只是一個快捷方式
getLast: function() {
return (this.length) ?this[this.length -1] : null;
},
//隨機獲取數組的一項
getRandom: function() {
return (this.length) ?this[$random(0, this.length -1)] : null;
},
//將指定項包含到數組,僅當數組中不存在該項時才會添加,通常用于不允許重復項的數組處理
include: function(item) {
if (!this.contains(item)) this.push(item);
returnthis;
},
//合并數組,并且保證數組的項不重復
combine: function(array) {
for (var i =0, l = array.length; i < l; i++) this.include(array[i]);
returnthis;
},
//在數組中刪除指定項
erase: function(item) {
for (var i =this.length; i--; i) {
if (this[i] === item) this.splice(i, 1); //注意用的是===
}
returnthis;
},
//清空數組
empty: function() {
this.length =0;
returnthis;
},
/*
將數組降維,即當數組中的項為數組時,將期拆開,按順序加到新數組中
這個在不能確定某個變量是數組還是單項時特別有用,比如說,我可以傳[1,2,3],也可以傳[1,[2,3]]
*/
flatten: function() {
var array = [];
for (var i =0, l =this.length; i < l; i++) {
var type = $type(this[i]);
if (!type) continue;
array = array.concat((type =='array'|| type =='collection'|| type =='arguments') ? Array.flatten(this[i]) : this[i]);
}
return array;
},
//顏色轉換,從十六進制轉為RGB表示
hexToRgb: function(array) {
if (this.length !=3) returnnull;
var rgb =this.map(function(value) {
if (value.length ==1) value += value;
return value.toInt(16);
});
return (array) ? rgb : 'rgb('+ rgb +')';
},
//顏色轉換,從RGB轉為十六進制表示
rgbToHex: function(array) {
if (this.length <3) returnnull;
if (this.length ==4&&this[3] ==0&&!array) return'transparent';
var hex = [];
for (var i =0; i <3; i++) {
var bit = (this[i] -0).toString(16);
hex.push((bit.length ==1) ?'0'+ bit : bit);
}
return (array) ? hex : '#'+ hex.join('');
}
});
Array.implement({
//迭代方法,call的使用
forEach: function(fn, bind) {
for (var i =0, l =this.length; i < l; i++) fn.call(bind, this[i], i, this);
}
});
//將each作為forEach的別名
Array.alias('forEach', 'each');
//轉為數組的快捷方式,但是在IE下,對于XML對象使用XPath查詢后的結果,$A方法無法達到預期的結果
function $A(iterable) {
if (iterable.item) { //對于collection,使用for遍歷復制到數組
var array = [];
for (var i =0, l = iterable.length; i < l; i++) array[i] = iterable[i];
return array;
}
//對于arguments和array對象,使用本方法可以轉為數組
return Array.prototype.slice.call(iterable);
};
//通用迭代遍歷方法
function $each(iterable, fn, bind) {
var type = $type(iterable);
((type =='arguments'|| type =='collection'|| type =='array') ? Array : Hash).each(iterable, fn, bind);
};
/*
對Array的擴展實現
對于迭代遍歷時,給fn傳遞的參數依次為:當前數組項,當前項的索引值,當前數組對象,比如:
7前逢單7后雙,判斷月份大小的算法
function myfilter(item, i, arr){
var half = arr.length / 2 + 1;
return half > i && item % 2 || half <= i && !(item % 2);
}
[1,2,3,4,5,6,7,8,9,10,11,12].filter(myfilter);
上面的代碼將返回[1, 3, 5, 7, 8, 10, 12]
然而,很多時候我們只需要使用第一個參數,即當前項的值
*/
Array.implement({
//對數組的每一項使用一個邏輯判斷,僅當所有項通過邏輯判斷時返回true
every: function(fn, bind) {
for (var i =0, l =this.length; i < l; i++) {
if (!fn.call(bind, this[i], i, this)) returnfalse;
}
returntrue;
},
//對數組進行邏輯過濾,返回包含所有通過邏輯判斷的項的數組
filter: function(fn, bind) {
var results = [];
for (var i =0, l =this.length; i < l; i++) {
if (fn.call(bind, this[i], i, this)) results.push(this[i]);
}
return results;
},
//配合$defined和filter方法,清除數組中的空項/
clean: function() {
returnthis.filter($defined);
},
/*
類似String對象的indexOf方法,返回數組在指定位置開始查找指定的匹配項,并返回其索引值,沒有找到時返回-1
from參數可以在0和數組長度-1之間取值,也可以為負值,當為負值時表示開始查找的位置從后往前的位數
*/
indexOf: function(item, from) {
var len =this.length;
for (var i = (from <0) ? Math.max(0, len + from) : from ||0; i < len; i++) {
if (this[i] === item) return i; //注意這里用的是絕對等于===比較
}
return-1;
},
//對數組中的每項進行處理,fn函數的返回值將代表原位置的項,最后返回一個經處理后的新數組
map: function(fn, bind) {
var results = [];
for (var i =0, l =this.length; i < l; i++) results[i] = fn.call(bind, this[i], i, this);
return results;
},
//與every方法相反,只需要數組中的有一項滿足條件就返回true
some: function(fn, bind) {
for (var i =0, l =this.length; i < l; i++) {
if (fn.call(bind, this[i], i, this)) returntrue;
}
returnfalse;
},
//將兩個數組關聯,組成鍵-值表示的對象,注意數組充當鍵還是值的決定因素--當前數組的每項作為值
associate: function(keys ){
var ōbj = {}, length = Math.min(this.length, keys.length);
for (var i =0; i < length; i++) obj[keys[i]] =this[i];
return obj;
},
//將數組的每項使用參數對象的方法進行處理,當返回true時當前項被添加到鍵值對象中作為值,處理的方法名作為鍵
link: function(object) {
var result = {};
for (var i =0, l =this.length; i < l; i++) {
for (var key in object) {
if (object[key](this[i])) {
result[key] =this[i]; //鍵和值的關系
delete object[key]; //保證result中鍵不重復,數組中索引值越小優先級越高
break;
}
}
}
return result;
},
//利用index方法判斷數組中是否包含指定項
contains: function(item, from) {
returnthis.indexOf(item, from) !=-1;
},
//擴展數組,將指定數組加到當前數組后面
extend: function(array){
for (var i =0, j = array.length; i < j; i++) this.push(array[i]);
returnthis;
},
//獲取數組的最后一項,只是一個快捷方式
getLast: function() {
return (this.length) ?this[this.length -1] : null;
},
//隨機獲取數組的一項
getRandom: function() {
return (this.length) ?this[$random(0, this.length -1)] : null;
},
//將指定項包含到數組,僅當數組中不存在該項時才會添加,通常用于不允許重復項的數組處理
include: function(item) {
if (!this.contains(item)) this.push(item);
returnthis;
},
//合并數組,并且保證數組的項不重復
combine: function(array) {
for (var i =0, l = array.length; i < l; i++) this.include(array[i]);
returnthis;
},
//在數組中刪除指定項
erase: function(item) {
for (var i =this.length; i--; i) {
if (this[i] === item) this.splice(i, 1); //注意用的是===
}
returnthis;
},
//清空數組
empty: function() {
this.length =0;
returnthis;
},
/*
將數組降維,即當數組中的項為數組時,將期拆開,按順序加到新數組中
這個在不能確定某個變量是數組還是單項時特別有用,比如說,我可以傳[1,2,3],也可以傳[1,[2,3]]
*/
flatten: function() {
var array = [];
for (var i =0, l =this.length; i < l; i++) {
var type = $type(this[i]);
if (!type) continue;
array = array.concat((type =='array'|| type =='collection'|| type =='arguments') ? Array.flatten(this[i]) : this[i]);
}
return array;
},
//顏色轉換,從十六進制轉為RGB表示
hexToRgb: function(array) {
if (this.length !=3) returnnull;
var rgb =this.map(function(value) {
if (value.length ==1) value += value;
return value.toInt(16);
});
return (array) ? rgb : 'rgb('+ rgb +')';
},
//顏色轉換,從RGB轉為十六進制表示
rgbToHex: function(array) {
if (this.length <3) returnnull;
if (this.length ==4&&this[3] ==0&&!array) return'transparent';
var hex = [];
for (var i =0; i <3; i++) {
var bit = (this[i] -0).toString(16);
hex.push((bit.length ==1) ?'0'+ bit : bit);
}
return (array) ? hex : '#'+ hex.join('');
}
});
總結
以上是生活随笔為你收集整理的[转帖]Mootools源码分析-04 -- Array的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: asp.net远程调用WebServic
- 下一篇: 艾伟:MOSS 2007 项目的开发步骤