Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rdev5
Last active November 8, 2017 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdev5/3c1075a7f2a38db2a9e03f7c44364378 to your computer and use it in GitHub Desktop.
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)
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