You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: readme.md
+17-5Lines changed: 17 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -182,23 +182,35 @@ input values, and does not produce side effects.
182
182
constgreet= (name) =>'Hi, '+ name
183
183
184
184
greet('Brianne') // 'Hi, Brianne'
185
-
186
185
```
187
186
188
-
As opposed to:
187
+
As opposed to each of the following:
189
188
190
189
```js
191
-
192
-
let greeting
190
+
window.name='Brianne'
193
191
194
192
constgreet= () => {
195
-
greeting ='Hi, '+window.name
193
+
return'Hi, '+window.name
196
194
}
197
195
198
196
greet() // "Hi, Brianne"
197
+
```
198
+
199
+
The above example's output is based on data stored outside of the function...
199
200
201
+
```js
202
+
let greeting
203
+
204
+
constgreet= (name) => {
205
+
greeting ='Hi, '+ name
206
+
}
207
+
208
+
greet()
209
+
greeting // "Hi, Brianne"
200
210
```
201
211
212
+
... and this one modifies state outside of the function.
213
+
202
214
## Side effects
203
215
204
216
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