当前位置: 代码迷 >> JavaScript >> javascript权威指南的1点总结
  详细解决方案

javascript权威指南的1点总结

热度:310   发布时间:2013-02-28 11:33:09.0
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);














  相关解决方案