I am new to Javascript and got confused by how the function declaration works. I made some test on that and got some interesting results:
say();
function say()
{
alert("say");
}
The forward-declaration worked and popup "say"
On the opposite
say();
say = function()
{
alert("say");
}
did not work, although it also declared a function object
If we declare the function and redeclare that afterwards:
function say()
{
alert("speak");
}
say();
function say()
{
alert("say");
}
I got "say" instead of "speak". That's surprise!
OK. It seems that only the latest function declaration works. Then lets declare function object first and then a "regular" function:
say = function()
{
alert("speak");
}
say();
function say()
{
alert("say");
}
say();
Another surprise, it was "speak" followed by "speak". The "regular" function declaration did not work at all!
Is there an explanation of all of them? And, if the "regular" function declaration is really that "fragile" and can be easily override by the function object with same name, should I stay away from that?
Another question is: with only the function object format, does that forward-declaration become impossible? Is there any way to "simulate" that in Javascript?