r/javascript • • Aug 27 '16

Is your JavaScript function actually pure?

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

48 comments sorted by

View all comments

2

u/robpalme Aug 27 '16

It's easy for JS education to descend into shock and awe on how the language can be subverted or is unintuitive. The phrase "You don't know JS" is easy to deploy.

To demonstrate, here's another object you could pass into the protected sum() function to force it to be impure despite the checks...

var x = [0];
Object.defineProperty(x, 1, { get: ()=>x[0]++ });
sum(x);  // 1
sum(x);  // 5