Created
January 3, 2017 00:42
-
-
Save ericelliott/c1aad9d5c13b0147630cb75e29a5b920 to your computer and use it in GitHub Desktop.
Timing dependency
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// With shared state, the order in which function calls are made | |
// changes the result of the function calls. | |
const x = { | |
val: 2 | |
}; | |
const x1 = () => x.val += 1; | |
const x2 = () => x.val *= 2; | |
x1(); | |
x2(); | |
console.log(x.val); // 6 | |
// This example is exactly equivalent to the above, except... | |
const y = { | |
val: 2 | |
}; | |
const y1 = () => y.val += 1; | |
const y2 = () => y.val *= 2; | |
// ...the order of the function calls is reversed... | |
y2(); | |
y1(); | |
// ... which changes the resulting value: | |
console.log(y.val); // 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment