Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

functional programming - Which functions in Array.prototype are pure function in Javascript

I know the the pure functions has some rules below.

  1. no side effect.
  2. always same input & same out when you call this function.

could someone help me list out the all pure functions in Javascript until ES2020?

I know slice is a pure function

Array.prototype.slice

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Array static methods

Array instance methods

* These accept a callback parameter. While the method will not itself alter any of the arguments, the callback might. So, technically it's possible to pass an impure callback which makes the whole operation impure.

** Returns an iterator. The operation is pure but using the iterator is not. For example, you might get different results if you get all the values of the iterator immediately or you get some, then the array is altered in-place, then you continue getting values from the iterator.

*** Technically pure by its operation but the expectation is that the callback it accepts will have side effects. If the callback doesn't have side effects, it's likely not going to be a useful operation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...