This isn't a "self-modifying" function in the strict sense - you don't modify any code on the fly (and that would be bad), just assign another value to a function name. This is fine in most cases and makes polymorphic code quite simple and elegant. To make the intent cleaner, you can factor out the "first time" code into a separate function:
function firstTimeFoo() {
.....
foo = normalFoo
}
function normalFoo() {
...
}
foo = firstTimeFoo