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. py2exe把python程序转换exe

    1.首先下载py2exe:https://sourceforge.net/projects/py2exe/ 2.假设要打包的python 文件放在C:\packet路径下 如 果你有一个名为myscr ...

  2. Collection和Collections的区别

    Collection 是集合类的上级接口,继承它的接口主要有set和list.Collections 是针对集合类的一个帮助类,他提供一系列静态方法实现对各种集合的搜索,排序,线程安全化等操作.

  3. VS Extension: Create a txt file and set the content

    使用 Visual Studio Extension 创建一个文本文件,并填入内容. 需要引用 EnvDTE C:\Program Files (x86)\Microsoft Visual Studi ...

  4. 记录android学习、开发过程温故知新

    记录android学习.开发过程温故知新

  5. 小米MIUI 360wifi掉线解决方案 落雨

    问: 360WIFI 小米WIFI 联网无网速的解决办法 方法1.退出电脑和手机上的360安全卫士(我抱着试试的态度退出了之后,我艹,速度立马有了!),估计是这些安全软件太强大,导致的. 方法2.我用 ...

  6. POJ3714+最近点对

    特判标记即可 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h& ...

  7. SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-002- 在xml中引用Java配置文件,声明DispatcherServlet、ContextLoaderListener

    一.所有声明都用xml 1. <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...

  8. S5P4418开发板使用要点

    一.调试工具 1)USB 转串口驱动:用于驱动板子的串口板,电脑与板子进行调试串口通信2)量产软件 USB驱动安装:用于 NXUsbBurner_Ver1.0.6 软件镜像烧写(量产工具)3)调试工具 ...

  9. java的异常处理机制(try…catch…finally)

    1 引子try…catch…finally恐怕是大家再熟悉不过的语句了,而且感觉用起来也是很简单,逻辑上似乎也是很容易理解.不过,我亲自体验的“教训”告诉我,这个东西可不是想象中的那么简单.听话.不信 ...

  10. 【HDOJ】1720 A+B coming

    水题. #include <stdio.h> #include <string.h> #define MAXNUM 1005 int stoi(char); int main( ...