Functions are values, and we can manipulate function values in interesting ways.Currying allows us to produce a new function by combining a function and an argument:

var add1 = add.curry(1);
document.writeln(add1(6)); //

add1 is a function that was created by passing 1 to add’s curry method. The add1 function adds 1 to its argument. JavaScript does not have a curry method, but we can fix that by augmenting Function.prototype:

Function.method('curry', function ( ) {
var args = arguments, that = this;
return function ( ) {
return that.apply(null, args.concat(arguments));
};
}); // Something isn't right...

The curry method works by creating a closure that holds that original function and the arguments to curry. It returns a function that, when invoked, returns the result of calling that original function, passing it all of the arguments from the invocation of curry and the current invocation. It uses the Array concat method to concatenate the two arrays of arguments together.

Unfortunately, as we saw earlier, the arguments array is not an array, so it does not have the concat method. To work around that, we will apply the array slice method on both of the arguments arrays. This produces arrays that behave correctly with the concat method:

Function.method('curry', function ( ) {
var slice = Array.prototype.slice,
args = slice.apply(arguments),
that = this;
return function ( ) {
return that.apply(null, args.concat(slice.apply(arguments)));
};
});

我想给所有的函数对象都添加curry方法:

function add(){
console.log(arguments);
}
add.curry();

运行结果:

> add.curry();
TypeError: Object function add(){
console.log('add');
} has no method 'curry'
at repl:1:5
at REPLServer.self.eval (repl.js:110:21)
at repl.js:249:20
at REPLServer.self.eval (repl.js:122:7)
at Interface.<anonymous> (repl.js:239:12)
at Interface.emit (events.js:95:17)
at Interface._onLine (readline.js:202:10)
at Interface._line (readline.js:531:8)
at Interface._ttyWrite (readline.js:760:14)
at ReadStream.onkeypress (readline.js:99:10)

其实add是一个函数对象,所有的函数都是Function的对象,它继承了Function.prototype:

Function.prototype['curry'] = function ( ) {
var slice = Array.prototype.slice,
args = slice.apply(arguments),
that = this;
return function ( ) {
return that.apply(null, args.concat(slice.apply(arguments)));
};
}

现在所有的方法对象都会有curry方法了:

> add2 = add.curry(1)
[Function]
> add2(2,3)
{ '0': 1, '1': 2, '2': 3 }
undefined
> add2(2,3,4)
{ '0': 1, '1': 2, '2': 3, '3': 4 }
undefined

JavaScript- The Good Parts function Curry的更多相关文章

  1. JavaScript自运行函数(function(){})()的理解

    今天打开JQuery源文件(jquery-1.8.3), 看到JQuery的初始化过程是这样的 (function( window, undefined ) { // .... })( window ...

  2. javascript对象模型和function对象

    javascript中,函数就是对象 <html> <head> <script type="text/javascript"> functio ...

  3. JavaScript: The Evil Parts - 1

    最近在看JavaScript框架设计,在讲解类型判定的时候提到了一些“匪夷所思的情况”,不过没有明说都是什么时候会出现这些情况.自己玩儿了一下,写写随笔吧.不过可能除了我找到的,还有会其他时候会出现这 ...

  4. JavaScript高级篇之Function对象

    JavaScript高级篇之Function对象 一: Function对象引入: Function对象是js的方法对象,可以用Function实例化出任何js方法对象. 例如: <%@ pag ...

  5. jsp中的javascript的$(document).ready( function() { $("#loginForm").validate()

    转自:https://bbs.csdn.net/topics/392459787?list=71147533 下面是jsp页面中的JavaScript代码 $(document).ready( fun ...

  6. [Javascript Crocks] Apply a function in a Maybe context to Maybe inputs (curry & ap & liftA2)

    Functions are first class in JavaScript. This means we can treat a function like any other data. Thi ...

  7. Javascript学习之函数(function)

    在JS中,Function(函数)类型实际上是对象;每个函数都是Function类型的实例,而且都与其他引用类型一样具有属性和方法.由于函数是对象,因此函数名实际上也是一个指向函数对象的指针. 一 函 ...

  8. JavaScript: The Good Parts

    Chapter 1 Good Parts: JavaScript is an important language because it is the language of the web brow ...

  9. javascript的 Object 和 Function

    一. javascript 的 内置对象: Object 和 Function javascript所有东西,包括 Function 都是对象 . Array  其实是一个 Function 类型的对 ...

随机推荐

  1. nodejs基础安装

    安装Nodejs需要从官网上下载一个最新的安装包,运行.我这里是win764位系统. 下载版本6.5.0 由于去外国的镜像上下载东西比较慢,淘宝为我们准备了国内的镜像.我们需要安装国内镜像的使用工具. ...

  2. MongoDB索引介绍

    MongoDB中的索引其实类似于关系型数据库,都是为了提高查询和排序的效率的,并且实现原理也基本一致.由于集合中的键(字段)可以是普通数据类型,也可以是子文档.MongoDB可以在各种类型的键上创建索 ...

  3. H5动画优化之路

    H5动画60fps之路 在移动端,和Native相比,H5一直都被人吐槽性能差,尤其是在动画方面. 谈到整个Web app的生命周期,一般分为四个部分: 加载 等待用户 响应用户 动画 一般情况下,首 ...

  4. 遍历DOM的所有节点,输出宽度高度都大于50的元素节点名称

    需要注意的问题有几点: 1.遍历所有元素节点的方式是:document.getElementsByTagName("*"),同时为了兼容性好可以再一句:document.all 2 ...

  5. VIM编辑命令的技巧

    vim 选择文本,删除,复制,粘贴   文本的选择,对于编辑器来说,是很基本的东西,也经常被用到,总结如下: v    从光标当前位置开始,光标所经过的地方会被选中,再按一下v结束. V    从光标 ...

  6. vs2012+opencv2.4.7 实现单张人脸识别

    参考:http://blog.sina.com.cn/s/blog_593c85f20100ncnj.html OpenCV的库中带有检测正面人脸的 Haar迭代算法Haar Cascade Face ...

  7. NEERC 2010, Eastern subregional contest

    只能把补了的题目放这儿了,先留个坑,怕忘记. Problem G URAL 1806 Mobile Telegraphs 题意是:给定n个电话号码,每个号码是一个长度为10的仅含'0'~'9'的字符串 ...

  8. QT4项目升级到QT5遇到的问题和解决方法

    QT4升级到QT5改动: PC部分: [改QTDIR变量] 在工程根目录下找到.user文件, 如InnoTabPlugin.vcxproj.user 修改指向你的QT5根目录: <Proper ...

  9. Tomcat 6.0下配置HTTPS

    最近项目需要使用到https,所以回顾整理了一下,其实在tomcat的文档中已经有了详细描述,我们启动Tomcat后,可以在docs文档中找到 地址如下:http://localhost:8080/d ...

  10. API:System V & POSI

    http://blog.sina.com.cn/s/blog_5b1572e30100gulz.html