日期:2014-05-16  浏览次数:20407 次

javascript权威指南的一点总结
function compose(f,g){
 return function(){
    return f.call(this,g.apply(this,arguments));
 };
}
var square=function(x){ return x*x;};
var sum=function(x,y){return x+y;};
var squareofsum=compose(square,sum);
var result=squareofsum(2,3);
console.log(result);

//--------以上javascript权威南指的方法----------
// ---下面综合运用xse:[1,2,3,4,5,6,7,8,9]----1*1+2*2+3*3+4*4+5*5+6*6+7*7+8*8+9*9--------
var map=Array.prototype.map
  ?function(a,f){return a.map(f);}
  :function(a,f){//自定义map
         var results=[];
         for(var i=0,len=a.length;i<len;i++){
           if(i in a){ results[i]=f.call(this,a[i],i,a);
		   }
		   return results;
  }
};
var funsum=function(a,f){
        var i=0,len=a.length,accumulator=0;
        while(i<len){
          if(i in a)
             accumulator=f.call(undefined,accumulator,a[i],i,a);
	         i++;
        }
     return accumulator;
};
var square=function(x){ return x*x;};
var arrays=[1,2,3,4,5,6,7,8,9];
var resultArrs=map(arrays,square);
console.log(resultArrs);
var sum=function(x,y){return x+y;};
var sumofarrays=funsum(resultArrs,sum);
console.log(sumofarrays);