简体   繁体   中英

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. 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.
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 .
Considerations?
Thanks.

You are referring to using Lodash as a "functional" library ( Read more about Lodash FP here) versus Lodash in its normal state. Functional languages, such as Haskell, follow the same methodology as the Lodash FP implementation. The benefits / cons are argued all over the internet, so I will leave that to other discussions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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