代码改变世界

JavaScript 学习笔记之Currying技术(转载)

2010-02-28 21:34  爱研究源码的javaer  阅读(182)  评论(0编辑  收藏  举报
John Resig在Pro Javascript一书中关于Currying的实现代码:

 

// A function that generators a new function for adding numbers
function addGenerator( num ) {

    
// Return a simple function for adding two numbers
    // with the first number borrowed from the generator
    return function( toAdd ) {
        
return num + toAdd
    };

}

// addFive now contains a function that takes one argument,
//
 adds five to it, and returns the resulting number
var addFive = addGenerator( 5 );

// We can see here that the result of the addFive function is 9,
//
 when passed an argument of 4
alert( addFive( 4 ) == 9 );
转自:http://www.cnblogs.com/sanshi/archive/2009/02/17/javascript_currying.html