Merge pull request #121 from alexlafroscia/update-purity-example

Update purity example
This commit is contained in:
hemanth.hm 2016-10-26 07:03:18 +05:30 committed by GitHub
commit 86d9603981

View file

@ -179,26 +179,36 @@ A function is pure if the return value is only determined by its
input values, and does not produce side effects. input values, and does not produce side effects.
```js ```js
const greet = (name) => 'Hi, ' + name const greet = (name) => `Hi, ${name}`
greet('Brianne') // 'Hi, Brianne' greet('Brianne') // 'Hi, Brianne'
``` ```
As opposed to: As opposed to each of the following:
```js ```js
window.name = 'Brianne'
let greeting const greet = () => `Hi, ${window.name}`
const greet = () => {
greeting = 'Hi, ' + window.name
}
greet() // "Hi, Brianne" 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('Brianne')
greeting // "Hi, Brianne"
```
... and this one modifies state outside of the function.
## Side effects ## 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. 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.