Skip to content

Commit ff29eb4

Browse files
committed
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.
1 parent e918b39 commit ff29eb4

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

readme.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,23 +182,35 @@ input values, and does not produce side effects.
182182
const greet = (name) => 'Hi, ' + name
183183

184184
greet('Brianne') // 'Hi, Brianne'
185-
186185
```
187186

188-
As opposed to:
187+
As opposed to each of the following:
189188

190189
```js
191-
192-
let greeting
190+
window.name = 'Brianne'
193191

194192
const greet = () => {
195-
greeting = 'Hi, ' + window.name
193+
return 'Hi, ' + window.name
196194
}
197195

198196
greet() // "Hi, Brianne"
197+
```
198+
199+
The above example's output is based on data stored outside of the function...
199200

201+
```js
202+
let greeting
203+
204+
const greet = (name) => {
205+
greeting = 'Hi, ' + name
206+
}
207+
208+
greet()
209+
greeting // "Hi, Brianne"
200210
```
201211

212+
... and this one modifies state outside of the function.
213+
202214
## Side effects
203215

204216
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.

0 commit comments

Comments
 (0)