Created
September 20, 2016 09:57
-
-
Save arvitaly/badf6cee1c62c993c654bfb705c5aa8b to your computer and use it in GitHub Desktop.
Example of change state in js-modules
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
var state = require('./state'); | |
var f1 = require('./f1'); | |
var f2 = require('./f2'); | |
try{ | |
f1();//Change state, really bad | |
var newState = f2();// pure function | |
//await f3 | |
//yield* f4 | |
}catch(e){ | |
} |
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
var state = require('./state'); | |
module.exports = ()=>{ | |
state.x = state.x + 1; | |
console.log(state.x); //side effect | |
} |
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
var state = require('./state'); | |
module.exports = ()=>{ | |
state = Object.assign({}, state); | |
state.y = state.y + 2; | |
return state; | |
} |
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
var state = { | |
x: 1, | |
y: 1 | |
} | |
module.exports = state; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment