Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.