何时在JavaScript中使用currying和partial函数 [英] When to use currying and partial functions in JavaScript

查看:91
本文介绍了何时在JavaScript中使用currying和partial函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Dr. Dobb关于JavaScript的currying和部分功能的内容中阅读了这篇帖子。它看起来很有用,但是我想知道(作为偶尔的JavaScript开发人员)是否存在常规使用的标准情况?

I read this post on Dr. Dobb's about currying and partial functions in JavaScript. It looks useful, but I'm wondering (as an occasional developer in JavaScript) if there are standard situations where this is regularly used?

推荐答案

对于初学者,我个人不建议在99%的情况下使用currying。如果使用不当,很容易使代码无法读取。

For starters, I personally wouldn't recommend currying in 99% cases. It can easily make code unreadable if used improperly.

但是,我可以命名的一些应用程序与设置函数上下文有关。例如,当您第一次使用上下文(窗口对象等以外的东西)执行currying函数时,您可以使用一个函数,在稍后调用时对原始对象的属性等应用某些计算。

However, some of the applications that I could name would be associated with setting the function context. For example when you first execute the currying function with a context (something other than window object etc), you could have a function that applies certain calculations on the original object's properties etc when called later.

第二种情况是,例如,你有一个带三个参数的函数。 DOM元素,属性名称及其值。这可以转换为可以使用适当的元素启动的currying函数,然后每次执行都会将属性设置为您希望的值。在有许多属性所依赖的条件的情况下,它可能很有用。

Second situation would be when, for example, you have a function that takes three arguments. The DOM element, the attribute name and it's value. This could be turned into a currying function that you could initiate with the appropriate element, then each following execution would set an attribute to the value you wish. Might be useful in cases, where you have numerous conditionals on which the attributes depend.

这篇关于何时在JavaScript中使用currying和partial函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆