What happens when a JavaScript function has a duplicate name? The experiment found that the page did not have any script error prompts, and that the program was running, except that the call to the function with the same name executed the next one.
Looking back carefully, this result is completely acceptable because the script is executed sequentially in the page itself, including the definition of the function, but if we simply define the form of function foo () {}, we will not be able to track the initialization of functions. However, if you are defining a class, we can clearly track the initialization order of the function. Like what:
function foo() {}
function foo.prototype.fn1() {}
function foo.prototype.fn2() {}
We can see clearly that performing function foo.prototype.fn1 () {} Before performing function foo.prototype.fn2 () {}.
Back to the problem with the name of the JavaScript script function We just said, like we define two functions Funalert ():
function funAlert()
{
alert('A');
}
function funAlert()
{
alert('B');
}
Call Funalert (), a megbox is displayed and the content is ' B '.
Why does the initialization function have this effect? The only way to change the definition of the above two functions is to make it clear that we'll change the definition to:
var fnAlert = new Function( " alert('A') " );
var fnAlert = new Function( " alert('B') " );
Window.fnalert ();
Its function is defined as a function pointer on the object, we like the pointer to what function reference, it performs what effect, and JavaScript script function name is the same as ordinary assignment statement, equivalent to: Var i=0;
var I=1;
Take a little notice of Var in javascript below, defining variables with Var is different from the high-level language definition variables we normally use, and it only acts as a hint, reminding me that I define variables here, and there is no concept of variable scope, as long as you don't leave the domain of the object that defines it ( such as page refresh), the variables that appear will always exist. So var can write without writing. As an example:
if(true)
{
t=100 ;
}
alert(t);
will show 100, while
if (true)
{
var t=100 ;
}
alert(t);
are shown also 100.
Therefore, the JavaScript script function name duplication is only an operational problem, and our high-level language grammar constraints are not the same thing, of course, but also not the scope of overload.
What's the use of a script function name? The most intuitive is that can be used to achieve pseudo overload, such as our many free home space will often impose pop-up ads for you, we can write on the first line of the page:
< script language ="javascript" >
var _open = window.open;
window.open=function () {}
</script >
So you can block the pop-up ads that are not added to the first line of the page (added to the first line can not be intercepted, because the window.open has not been ' overloaded ' open before execution).