简体   繁体   English

我是否应该优先将数组或函数作为JavaScript中的参数传递

[英]Should I prefer passing the array or the function first as arguments in JavaScript

I came across the filter function in Lodash and I was surprised to see the array as the first argument while the function as the second argument. 我在Lodash中遇到了filter函数,我很惊讶地看到数组是第一个参数,而函数是第二个参数。 Shouldn't be the opposite? 不应该相反吗? Is there a reason behind it? 背后有原因吗?

UPDATE: 更新:
The few programming languages I know are using the filter() method the opposite way passing the function as the first argument and the array as the second argument. 我知道的几种编程语言都使用filter()方法,其相反方式是将函数作为第一个参数,而将数组作为第二个参数。
So I was just curious about the origin of this. 所以我只是对此感到好奇。 Are there other languages or libraries that prefer passing the array/values first and the function after, even in functional programming operations like filtering (I'm not talking about callback functions). 是否还有其他语言或库更喜欢先传递数组/值,然后再传递函数,即使在诸如过滤之类的功能编程操作中(我不是在谈论回调函数)。
One advantage of passing array/values as the last argument is that I can take easily advantage of Destructuring . 将数组/值作为最后一个参数传递的一个优势是,我可以轻松利用Destructuring的优势。
Considerations? 注意事项?
Thanks. 谢谢。

You are referring to using Lodash as a "functional" library ( Read more about Lodash FP here) versus Lodash in its normal state. 您指的是将Lodash用作“功能性”库( 此处有关于Lodash FP的更多信息),而Lodash处于正常状态。 Functional languages, such as Haskell, follow the same methodology as the Lodash FP implementation. 功能语言(例如Haskell)遵循的方法与Lodash FP实现相同。 The benefits / cons are argued all over the internet, so I will leave that to other discussions. 优点/缺点在整个互联网上都有争议,因此我将其留给其他讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM