javascript権威の手引きの少しの総括

1152 ワード

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);