jquery 源码学习

jquery:grep 函数
grep: function( elems, callback, inv ) {
var ret = [], retVal;
inv = !!inv; //变成boolen, 默认是false
 
// Go through the array, only saving the items
// that pass the validator function
for ( var i = 0, length = elems.length; i < length; i++ ) {
retVal = !!callback( elems[ i ], i );  // 将满足callback函数中的条件的值返回
if ( inv !== retVal ) {
ret.push( elems[ i ] );
}
}
 
return ret;
},
 
例子 :
$.grep([0,1,2],function(n,i){
   
return n >0;
 
});

Result:

[1, 2] 
 
 
 
 
 
posted @ 2012-02-22 14:17  风去无痕  阅读(158)  评论(0编辑  收藏  举报