Skip to content

Instantly share code, notes, and snippets.

@ericelliott
Created January 3, 2017 00:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericelliott/c1aad9d5c13b0147630cb75e29a5b920 to your computer and use it in GitHub Desktop.
Save ericelliott/c1aad9d5c13b0147630cb75e29a5b920 to your computer and use it in GitHub Desktop.
Timing dependency
// 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