Java Reference
In-Depth Information
Functions that Define and Rewrite Themselves
The dynamic nature of JavaScript means that a function is able to not only call itself, but
define itself, and even redefine itself. This is done by assigning a variable to an anonymous
function that has the same name as the function.
Consider the following function:
function party(){
console.log("Wow this is amazing!");
party = function(){
console.log("Been there, got the T-Shirt");
}
}
This logs a message in the console, then redefines itself to log a different message in the
console. When the function has been called once, it will be as if it was defined like this:
function party() {
console.log("Been there, got the T-Shirt");
}
Every time the function is called after the first time, it will log the message " Been there,
got the T-Shirt ":
party();
<< "Wow this is amazing!"
party();
<< "Been there, got the T-Shirt"
party();
<< "Been there, got the T-Shirt"
If the function is also assigned to another variable, then this variable will maintain the
original function definition and not be rewritten. This is because the rewriting depends
on the name of the function. You can see an example of this if we create variable called
Search WWH ::




Custom Search