jeudi 17 décembre 2015

Simplest self-modifying function in JavaScript?

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?





Aucun commentaire:

Enregistrer un commentaire