JavaScript- The Good Parts function Curry

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

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
时间: 2024-10-27 09:46:42

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

JavaScript: The Evil Parts - 1

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

javascript对象模型和function对象

javascript中,函数就是对象 <html> <head> <script type="text/javascript"> function add(number){ alert(number+20); } var add=function(number){ alert(number+20); } function add(number,number1){ alert(number+30); } var add=function(number)

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

今天打开JQuery源文件(jquery-1.8.3), 看到JQuery的初始化过程是这样的 (function( window, undefined ) {     // .... })( window ); 一开始看不懂这个写法, 经过几番搜索终于明白它的用法以及为什么这样用了, 我们一步步来分析. 1, 首先我们简化这个写法 除去参数, 经过简化后的写法可以写成 (function(){     console.log("Hello World"); })(); 后面都使用这个

JavaScript Patterns 4.8 Function Properties - A Memoization Pattern

Gets a length property containing the number of arguments the function expects: function func(a, b, c) {} console.log(func.length); // 3 var myFunc = function () { // serialize the arguments object as a JSON string and use that string as a key in you

Javascript学习之函数(function)

在JS中,Function(函数)类型实际上是对象;每个函数都是Function类型的实例,而且都与其他引用类型一样具有属性和方法.由于函数是对象,因此函数名实际上也是一个指向函数对象的指针. 一 函数的声明方式 //1.函数声明方式 function add(num1,num2){ return num1+num2; } //2.函数表达式定义函数 var add= function(num1,num2){ // 通过变量box即可引用函数; return num1+num2; }; // 注

JavaScript: The Good Parts

Chapter 1 Good Parts: JavaScript is an important language because it is the language of the web browser. The very good ideas include functions, loose typing, dynamic objects, and an expressive object literal notation. The bad ideas include a programm

javascript的 Object 和 Function

一. javascript 的 内置对象: Object 和 Function javascript所有东西,包括 Function 都是对象 . Array  其实是一个 Function 类型的对象, 它的prototype 是指向了 Function.prototype . new Array()  是一个 Object 对象, 它的 prototype 指向了 Object.prototype . 函数的第一种定义方法: function sum1(a,b){              

【Javascript】—— 1 方法function的高级特性

本篇仅仅对于function作简单的讲解,在javascript中function不仅仅是方法,它其实是一个变量,因此拥有自己的属性,并且可以当做参数传递给其他的方法. 那么传统的方法,按照java的写法应该如下的创建: <!-- 普通的function --> function testFunc1(name,age){ console.log("name"+name+" age"+age); } testFunc1("xingoo"

【JavaScript】JS_Object跟Function的区别

JS_Object和Function的区别 我们本次的解释,主要通过下图 粗看该图,估计你不一定能看明白.不过接下来让我逐行向你解释. 最左侧:意思是,有两个对象f1和f2,他们是通过new Foo()出来的. 中间:Foo()就是最左侧用到的Foo() 最右侧:表示,函数Foo()的prototype属性! 1.__proto__的虚线:该条线表示通过f1.__proto__可以访问到Foo.prototype,其实两者是等价的. 2.constructor实现:表示通过Foo.prototy