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

本文共 1655 字,大约阅读时间需要 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');

转载地址:http://lbtpl.baihongyu.com/

你可能感兴趣的文章
掉电引起的ORA-1172错误解决过程(二)
查看>>
在网站建设过程中主要在哪几个方面为后期的网站优打好根基?
查看>>
【MOS】RAC 环境中最常见的 5 个数据库和/或实例性能问题 (文档 ID 1602076.1)
查看>>
新年图书整理和相关的产品
查看>>
Struts2的核心文件
查看>>
Spring Boot集成Jasypt安全框架
查看>>
GIS基础软件及操作(十)
查看>>
HDOJ 2041 超级楼梯
查看>>
1108File Space Bitmap Block损坏能修复吗2
查看>>
遭遇DBD::mysql::dr::imp_data_size unexpectedly
查看>>
人人都会设计模式:03-策略模式--Strategy
查看>>
被忽视但很实用的那部分SQL
查看>>
解读阿里云oss-android/ios-sdk 断点续传(多线程)
查看>>
ML之监督学习算法之分类算法一 ——— 决策树算法
查看>>
骡夫电商地址
查看>>
亚信安全火力全开猎捕“坏兔子”,全歼详解
查看>>
[二]RabbitMQ-客户端源码之AMQConnection
查看>>
通过添加HTTP Header实现上下文数据在WCF的自动传递
查看>>
Emacs之魂(三):列表,引用和求值策略
查看>>
【ARM】一步一步移植Linux Kernel 2.6.13到板子
查看>>