首页 > 代码库 > [Ramda] Convert Object Methods into Composable Functions with Ramda
[Ramda] Convert Object Methods into Composable Functions with Ramda
In this lesson, we‘ll look at how we can use Ramda‘s invoker
and constructN
functions to take methods of an object and turn them into reusable utility functions that are curried and accept their object as the last argument. We‘ll convert a dot-chained string of jQuery methods and create a composed function that can be applied to multiple elements.
const {invoker, compose, constructN} = R // $(‘#sample‘) // .animate({left:‘250px‘}) // .animate({left:‘10px‘}) // .slideUp() const animate = invoker(1, ‘animate‘) const slide = invoker(0, ‘slideUp‘) const jq = constructN(1, $) const animateDiv = compose( slide, animate({left:‘10px‘}), animate({left:‘250px‘}), jq ) animateDiv(‘#sample‘) animateDiv(‘#sample2‘)
<iframe style="width: 100%; height: 500px;" src="https://embed.plnkr.co/GZSpexVstzUGIluh6fNQ/" width="320" height="240"></iframe>
[Ramda] Convert Object Methods into Composable Functions with Ramda
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。