r/Frontend • • Sep 19 '16

Is your JavaScript function actually pure? Or does this function behaves like a Mathematical Function in all the situations I will encounter in my code?

http://staltz.com/is-your-javascript-function-actually-pure.html
2 Upvotes

3 comments sorted by

1

u/[deleted] Sep 19 '16

That's not even the correct definition of a pure function...

1

u/Idiomatic-Oval Oct 05 '16

A pure function is a function where the return value is only determined by its input, without observable side effects.

It fits fine. He made a function sum, which when invoked with the exact same input gives different results. Therefore there must be some other state involved in determining the output. Therefore there is some observable side effect. Just like a function that gets the current time can never be pure.

I didn't think the example could be impure. I didn't know about Object.prototype.valueOf. A new thing to screw with my co-workers.

0

u/fagnerbrack Sep 19 '16

This is a x-post from /r/javascript.