博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Javascript] What is JavaScript Function Currying?
阅读量:4983 次
发布时间:2019-06-12

本文共 1709 字,大约阅读时间需要 5 分钟。

Currying is a core concept of functional programming and a useful tool for any developer's toolbelt. 

 

Example 1:

let f = a => b => c => a+b+c;let result = f(1)(2)(3);console.log(result); // 6

 

Example 2:

  
JS Bin
One
Two
Three
let one = document.getElementById('one');let tow = document.getElementById('two');let three = document.getElementById('three');let f = a => b => c => a.addEventListener(b, (event)=>{  event.target.style.background = c;});f(one)('click')('red');

 

Example 3: 

let one = document.getElementById('one');let tow = document.getElementById('two');let three = document.getElementById('three');let f = a => b => c => a.addEventListener(b, (event)=>{  event.target.style.background = c;});let oneClickEvent = f(one);let twoClickEvent = f(two);oneClickEvent('mouseover')('red');twoClickEvent('mouseout')('blue');

 

Example 4: include ramda library:

let one = document.getElementById('one');let tow = document.getElementById('two');let three = document.getElementById('three');let f = R.curry((a,b,c) =>{  a.addEventListener(b, (event)=>{    event.target.style.background = c;  });});// with placeholder from Ramdaconst clickGreen = f(R.__, 'click', 'green');clickGreen(one);

 

Example 5: multi placeholders:

let one = document.getElementById('one');let tow = document.getElementById('two');let three = document.getElementById('three');let f = R.curry((a,b,c) =>{  a.addEventListener(b, (event)=>{    event.target.style.background = c;  });});// with placeholder from Ramdaconst clickColor = f(R.__, 'click', R.__);clickColor(one, 'yellow');clickColor(two, 'grey');clickColor(three, 'pink');

转载于:https://www.cnblogs.com/Answer1215/p/5003880.html

你可能感兴趣的文章
C#wxpay和alipay
查看>>
Combination Sum
查看>>
WCF开发框架形成之旅---结合代码生成工具实现快速开发
查看>>
Spring事务管理
查看>>
JS||JQUERY常用语法
查看>>
talend hive数据导入到mysql中
查看>>
ORA-01093: ALTER DATABASE CLOSE only permitted with no sessions connected
查看>>
linux下mysql配置文件my.cnf详解
查看>>
获取微信用户列表Openid
查看>>
架构必备词汇
查看>>
SublimeText快捷键操作
查看>>
Python开发 基礎知識 (未完代補)
查看>>
监听器的使用,以及实现, 测试
查看>>
java基础二 分支循环
查看>>
python--002--数据类型(list、tuple)
查看>>
把近期的小错误整理一下
查看>>
动态规划 —— 背包问题一 专项研究学习
查看>>
51nod 1571 最近等对 | 线段树 离线
查看>>
关于parseInt的看法
查看>>
从用户端到后台系统,严选分销教会我这些事
查看>>