for ( var type inExpr.match ) { Expr.match[ type ] = newRegExp( Expr.match[ type ].source + (/(?![^\[]*\])(?![^\(]*\))/.source) ); Expr.leftMatch[ type ] = newRegExp( /(^(?:.|\r|\n)*?)/.source + Expr.match[ type ].source.replace(/\\(\d+)/g, fescape) ); }
varSizzle = function( selector, context, results, seed ){ var soFar = selector, extra ,//extra用来保存并联选择的其他部分,一次只处理一个表达式 parts = [], m; do { chunker.exec( "" ); //这一步主要是将chunker的lastIndex重置,当然直接设置chunker.lastIndex效果也一样 m = chunker.exec( soFar ); if ( m ) { soFar = m[3]; parts.push( m[1] ); if ( m[2] ) { //如果存在并联选择器,就中断,保存其他的选择器部分。 extra = m[3]; break; } } } while ( m ); }
// Here we check if the JavaScript engine is using some sort of // optimization where it does not always call our comparision // function. If that is the case, discard the hasDuplicate value. // Thus far that includes Google Chrome. [0, 0].sort(function() { baseHasDuplicate = false; return0; });
functionzip(){ var array_1 = [1,2,3]; var array_2 = ['a','b','c']; var array_3 = ['x','y','z']; var result = [array_1].concat([array_2,array_3]);//[[1,2,3],['a','b','c'],['x','y','z']] return array_1.map(function(value,index){ return result.pluck(index); }) } console.log(zip());//[[1,'a','x'],[2,'b','y'],[3,'c','z']]
改为Enumerable方法的形式就是:
1 2 3 4 5 6 7
functionzip(){ var result = [this].concat(Array.prototype.slice.call(arguments,1)); returnthis.map(function(value,index){ return result.pluck(index); }) } console.log([1,2,3].zip(['a','b','c'],['x','y','z']));////[[1,'a','x'],[2,'b','y'],[3,'c','z']]
加上interator处理之后就是:
1 2 3 4 5 6 7 8 9 10 11
functionzip(){ var args = Array.prototype.slice.call(arguments,0); var interator = function(x){ return x;} //Prototype.K if(args[args.length - 1].constructor == Function){ interator = args.pop(); } var result = [this].concat(Array.prototype.slice.call(arguments,1)); returnthis.map(function(value,index){ return interator.call(result.pluck(index)); }) }
var each = Array.prototype.forEach || function(iterator,context){ for(var i = 0,len = this.length ; i < len ; i++){ iterator.call(context,this[i],this); } };
按照上面的方法,我们给Array对象扩展一个打印当前所有元素的print方法。
1 2 3 4 5 6 7 8 9 10 11
Array.prototype.each = Array.prototype.forEach || function(iterator,context){ for(var i = 0,len = this.length ; i < len ; i++){ iterator.call(context,this[i],i,this); } }; Array.prototype.print = function(){ this.each(function(item){ console.log(item); }); } console.log([1,2,3,4].print());//1,2,3,4