This article brings you relevant knowledge about javascript, which mainly introduces the related issues of function currying in JavaScript. Currying is to transform a function that accepts multiple parameters. It is a technique that accepts a single parameter and returns a new function that accepts the remaining parameters and returns the result. I hope it will be helpful to everyone.
Related recommendations: javascript tutorial
1. Briefly understand apply and call
- call and apply both exist to change the context when a function is running. In other words, they are to change the pointer of this inside the function body.
- call and apply have exactly the same function, but the way they accept parameters is different. call is actually a kind of syntactic sugar for apply.
- Format:
apply(context,[arguments])
,call(context,param1,param2,...)
.
2. What is function currying?
Currying is to transform a function that accepts multiple parameters into a function that accepts a single parameter (the first parameter of the original function), and returns a new function that accepts the remaining parameters and returns the result. Technology.
For example, there is a add()
function, which is a function used to process the addition and sum of the parameters (param1, params2,...) we pass to it. .
// 在这里第一个具有两个参数`x`、`y`的`add(x , y)`函数 function add(x , y){ return x + y; } // 调用`add()`函数,并给定两个参数`4`和`6` add(4,6); // 模拟计算机操作,第一步 传入第一个参数 4 function add(4 , y){ return 4 + y; } // 模拟计算机操作,第二步 传入第一个参数 6 function add(4 , 6){ return 4 + 6; }
What would it look like if we curried the add()
function? Here is a simple implementation:
// 柯里化过的add()函数,可以接受部分参数 function add(x ,y){ if (typeof y === 'undefined') { return function (newy){ return x + newy; } } // 完整应用 return x + y; } // 测试调用 console.log(typeof add(4)); // [Function] console.log(add(4)(6)); // 10 // 可以创建保存函数 let saveAdd = add(4); console.log(saveAdd(6)); // 10
As can be seen from the above simple curried add()
function, the function can accept some functions and then return a new function to make it Continue with the remaining functions.
3. Write a public curried function
Here we create a public curried function, so that we don’t have to implement complex functions internally every time we write a function The currying process.
// 定义一个createCurry的函数function createCurry(fn){ var slice = Array.prototype.slice, stored_args = slice.call(arguments,1); return function () { let new_args = slice.call(arguments), args = stored_args.concat(new_args); return fn.apply(null,args); }}
In the above public curried function:
-
arguments
, is not a real array, just an array withlength
Attribute object, so we borrow theslice
method fromArray.prototype
to help us convertarguments
into a real array to facilitate our better operations . - When we call function
createCurry
for the first time, the variablestored_args
holds the parameters except the first parameter, because the first parameter is We need curried functions. - When we execute the function returned in the
createCurry
function, the variablenew_args
gets the parameters and converts them into an array. - The function returned internally accesses the value stored in the variable
stored_args
through a closure and the value of the variablenew_args
is combined into a new array and assigned to the variableargs
. - Finally call the
fn.apply(null,args)
method to execute the curried function.
Now let’s test the public currying function
// 普通函数add() function add(x , y){ return x + y; } // 柯里化得到一个新的函数 var newAdd = createCurry(add,4); console.log(newAdd(6)); // 10 //另一种简便方式 console.log(createCurry(add,4)(6));// 10
Of course, this is not limited to currying of two parameters, but can also be multiple Parameters:
// 多个参数的普通函数 function add(a,b,c,d){ return a + b + c + d; } // 柯里化函数得到新函数,多个参数可以随意分割 console.log(createCurry(add,4,5)(5,6)); // 20 // 两步柯里化 let add_one = createCurry(add,5); console.log(add_one(5,5,5));// 20 let add_two = createCurry(add_one,4,6); console.log(add_two(6)); // 21
Through the above example, we can find a limitation, that is, whether it is two parameters or multiple parameters, it can only be executed in two steps, such as the following formula:
- fn(x,y) ==> fn(x)(y);
- fn(x,y,z,w) ==> fn(x)(y,z, w) || fn(x,y)(z,w)||…
If we want to be more flexible:
- fn(x,y) = => fn(x)(y);
- fn(x,y,z) ==> fn(x,y)(z) || fn(x)(y)(z) ;
- fn(x,y,z,w) ==> fn(x,y)(z)(w) || fn(x)(y)(z)(w) || …;
How do we achieve it?
4. Create a flexible curried function
After the above exercises, we found that the curried function we created has certain limitations. We hope that the function can be executed in multiple steps:
// 创建一个可以多步执行的柯里化函数,当参数满足数量时就去执行它: // 函数公式:fn(x,y,z,w) ==> fn(x)(y)(z)(w); let createCurry = (fn,...params)=> { let args = parsms || []; let fnLen = fn.length; // 指定柯里化函数的参数长度 return (...res)=> { // 通过作用域链获取上一次的所有参数 let allArgs = args.slice(0); // 深度拷贝闭包共用的args参数,避免后续操作影响(引用类型) allArgs.push(...res); if(allArgs.length <p>We have implemented a flexible currying function above, but here we find another problem: </p>
- If I pass in all the parameters for the first time, but It does not return a result, but a function.
- Only if we call the returned function once again can the result be returned:
curryAdd(add,1,2,3,4)()
; - Some people may say that if To pass all parameters, just call the original
add()
function. This is also a way; but since we are here to meet the number of parameters, we still deal with this situation.
Here we only need to make a judgment before returning the function:
let createCurry = (fn,...params)=> { let args = parsms || []; let fnLen = fn.length; // 指定柯里化函数的参数长度 if(length === _args.length){ // 加入判断,如果第一次参数数量以经足够时就直接调用函数获取结果 return fn.apply(this,args); } return (...res)=> { let allArgs = args.slice(0); allArgs.push(...res); if(allArgs.length <p>The above can be regarded as completing a flexible currying function, but there is not yet The calculation is very flexible because we cannot control when it is executed. It will be executed automatically as long as the number of parameters is sufficient. What should we do if we want to achieve a timing that can control its execution? </p><h2 id="Write-a-curried-function-with-controllable-execution-time">5. Write a curried function with controllable execution time</h2><p> Let’s directly explain the function formula here: </p>
- fn(a,b,c) ==> fn(a)(b)(c )();
- fn(a,b,c) ==> fn(a);fn(b);fn(c );fn();
- 当我们参数足够时它并不会执行,只有我们再次调用一次函数它才会执行并返回结果。在这里我们在以上例子中加一个小小的条件就可以实现。
// 当参数满足,再次执行时调用函数 let createCurry = (fn,...params)=> { let args = parsms || []; let fnLen = fn.length; // 指定柯里化函数的参数长度 //当然这里的判断需要注释掉,不然当它第一次参数数量足够时就直接执行结果了 //if(length === _args.length){ // 加入判断,如果第一次参数数量以经足够时就直接调用函数获取结果 //return fn.apply(this,args); //} return (...res)=> { let allArgs = args.slice(0); allArgs.push(...res); // 在这里判断输入的参数是否大于0,如果大于0在判断参数数量是否足够, // 这里不能用 && ,如果用&& 也是参数数量足够时就执行结果了。 if(res.length > 0 || allArgs.length <p>相关推荐:<a href="https://www.php.cn/course/list/17.html" target="_blank">javascript学习教程</a><br></p>
The above is the detailed content of Let's talk about JavaScript function currying. For more information, please follow other related articles on the PHP Chinese website!

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
