I would like to research and fiddle around with self-modifying functions and knowing that JavaScript has eval
and other interesting reflection-ish features, like making a function print its body from the inside, I figured I would like to do some self-modifying function tests with JavaScript.
I want to have a simple function, that, when called, modifies itself according to what ever, any rule.
Like maybe some function could first do a simple console.log
and the next time it is called it would do alert
instead. This can be obviously achieved with an if
, but what I am looking for here is that the actual physical function should first look like this:
function selfm()
{
console.log('hello')
// + modify self here somehow
}
And after the first call:
function selfm()
{
alert('hello')
}
How can I achieve this kind of behavior in its simplest form?
I would like to research and fiddle around with self-modifying functions and knowing that JavaScript has eval
and other interesting reflection-ish features, like making a function print its body from the inside, I figured I would like to do some self-modifying function tests with JavaScript.
I want to have a simple function, that, when called, modifies itself according to what ever, any rule.
Like maybe some function could first do a simple console.log
and the next time it is called it would do alert
instead. This can be obviously achieved with an if
, but what I am looking for here is that the actual physical function should first look like this:
function selfm()
{
console.log('hello')
// + modify self here somehow
}
And after the first call:
function selfm()
{
alert('hello')
}
How can I achieve this kind of behavior in its simplest form?
Share asked Dec 17, 2015 at 8:16 SwiffySwiffy 4,7253 gold badges28 silver badges56 bronze badges 8- 1 Or you could just implement a function that accepts a selector function and an array of other functions to invoke. So the selector would choose which one to invoke, then it would be invoked. Clear, easy to maintain/test, no ugly runtime mutations. – zerkms Commented Dec 17, 2015 at 8:18
- 1 @zerkms That misses my point. I want to make a function that can literally edit itself from the inside when it is called, so that the actual structure of the function changes after the call. The point is also to pletely lose the information about what the function looked like before it was called at all. – Swiffy Commented Dec 17, 2015 at 8:21
- 2 It's not the problem - it's an ugly solution. Explain the original problem that requires such a weird solution. – zerkms Commented Dec 17, 2015 at 8:22
- 2 Just another example of the XY problem – hindmost Commented Dec 17, 2015 at 8:24
- 2 @zerkms The original "problem" is that I want to inspect how self-modifying functions work in JavaScript, but I cannot quite e up with a function that is, well, self-modifying. This is just me doing private fiddling, so the ugliness of the solution does not matter. – Swiffy Commented Dec 17, 2015 at 8:27
4 Answers
Reset to default 5This might not be a great solution, but hope it helps.
function selfm(str){
this.print = eval("(function a() {"+str+"})") ;
this.print();
}
And you can then call onto with whatever string parameter you wish your function to act as.
selfm("console.log('Hello');");
// will print Hello in console.
selfm("alert('Hello');");
// will alert Hello
Still need to test on different browsers. :)
If you only need it to change itself once, then you could do this:
var selfm = function () {
console.log('hello');
selfm = function () {
alert('hello');
};
};
The selfm() function would print "hello" to the console the first time, and then change itself into a function that instead shows a "hello" alert every time after that.
This could be useful in cases where there's some plex bit of code that needs to be run, but after it has run one time, every time after that it would produce the same result. You could optimize such a function to just immediately return that same result, instead of re-doing the calculations every time, by using that method.
//basic example
let myFunction=function(){
console.log("previous code!");
myFunction=new Function("console.log('new code!');");
}
myFunction(); //previous code!
myFunction(); //new code!
//Javascript self modifying function
function operation(operator,num1,num2){
//storing reference
let temp=operation;
let mod=`"console.log(num1${operator}num2)"`;
//performing modification
operation=new Function("num1,num2",eval(mod));
operation(num1,num2);
//restore original reference
operation=temp;
}
operation("+",10,20); //30
operation("-",20,10); //10
operation("*",10,20); //200
operation("/",20,10); //2