Update the Purity example code

Reading through this, the example code for the Purity information was a bit confusing.  The original example tried to show both using and changing global state in one function, but because of that the "pure" example and "unpure" example didn't actually do the same thing.

I think it's a little more clear if the two concepts are broken up, instead showing the "pure" version, one that uses global state, and one that modifies global state.
This commit is contained in:
Alex LaFroscia 2016-09-25 12:17:37 -07:00
parent e918b396ec
commit ff29eb4abd

View file

@ -182,23 +182,35 @@ input values, and does not produce side effects.
const greet = (name) => 'Hi, ' + name
greet('Brianne') // 'Hi, Brianne'
```
As opposed to:
As opposed to each of the following:
```js
let greeting
window.name = 'Brianne'
const greet = () => {
greeting = 'Hi, ' + window.name
return 'Hi, ' + window.name
}
greet() // "Hi, Brianne"
```
The above example's output is based on data stored outside of the function...
```js
let greeting
const greet = (name) => {
greeting = 'Hi, ' + name
}
greet()
greeting // "Hi, Brianne"
```
... and this one modifies state outside of the function.
## Side effects
A function or expression is said to have a side effect if apart from returning a value, it interacts with (reads from or writes to) external mutable state.