I have been programming for the Web for a long time, but only recently discovered a few new subtleties regarding the use of functions and the strange (or so I consider them as) things that you can do with them. However, at the moment they seem syntactically good. I was hoping that someone could enlighten me on how some of these newly discovered aspects might prove useful.
For example, the first time I ran this, I thought this would not work:
<script>
function x(q)
{
q(x);
}
x(function(a)
{
alert(a);
}
);
</script>
But it is so! One way or another, creating a named function that receives another anonymous function as the only parameter, and then runs the function passed to it with the one passed to it as the parameter, it works fine. This positively reflected my mind, and I am pretty sure that there is a lot of practicality in it, but I just can not completely place it.
Oh, and one more thing that I raised to discover: using a variable with a wide range region to store the function, you can later execute the JavaScript eval () function to change this variable, thereby dynamically changing the internal operation of the function. Example:
<script>
var f = function()
{
alert('old text');
}
eval('f = ' + f.toString().replace('old text', 'new text'));
f();
</script>
Of course, this code warns the line "new text"; when I saw this, my mind was blown up again, but also immediately intrigued to create something incredible.
... Stack Overflow: , , ?