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]