What is the advantage of using a function over loops?

前端 未结 3 1310
南旧
南旧 2021-01-26 07:52

It seems that functional iterators are replacing the use of for loops in JS.

What is the advantage of passing a function such as map or reduce

3条回答
  •  别那么骄傲
    2021-01-26 08:06

    It's a paradigm shift. The latter is a form of imperative programming, where the user is the one creating data that the computer consumes. The former is essentially a more functional based approach stemming from mathematics, utilizing data (code) that already exists.

    There is no theoretical (processing) advantage of one over the other, though applied to the current state of computers, functional programming becomes more useful as processing power becomes greater.

    Functional programming allows a form of reasoning based in mathematics emphasizing inputs and outputs. JavaScript in particular is good at handling this style due to functions being first-class data types

提交回复
热议问题