Last active
November 8, 2017 18:30
-
-
Save rdev5/3c1075a7f2a38db2a9e03f7c44364378 to your computer and use it in GitHub Desktop.
Basic example of self-modifying function in JS (https://en.wikipedia.org/wiki/Self-modifying_code)
This file contains 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
window.fx_update = 'window.fx = function() { setTimeout(function() { window.fx_modified = false; window.fx = window.fx_source; }, 5000); return "Modified state of fx() 5s later"; };'; | |
window.fx_source = function() { | |
if (!window.fx_modified) { | |
setTimeout(function() { | |
eval(window.fx_update); | |
}, 5000); | |
window.fx_modified = true; | |
} | |
return "Initial state of fx()"; | |
}; | |
window.fx_modified = false; | |
window.fx = window.fx_source; | |
// Run | |
window.fx(); | |
setInterval(function() { console.log(window.fx()) }, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment