The initialization method of JavaScript Functions

Source: Internet
Author: User

Today, a buddy asked me what would happen if I had repeated JavaScript Functions? I didn't think about it at first, so I thought it might be wrong, but when I finish the experiment, I found no Script Error prompt on the page, andProgramIt also runs, but executes a back-to-back call to a function with the same name.

Looking back, I think this result is acceptable, because the script is executed sequentially on the page, including the definition of the function, but if only function Foo () is defined () {} in this form, we cannot trace function initialization. However, if the class is defined, we can clearly track the initialization sequence of the function. For example:

Function Foo (){}
Function Foo. Prototype. fn1 (){}
Function Foo. Prototype. FN2 (){}

We can see that the function Foo. Prototype. fn1 () {} is executed first and then function Foo. Prototype. FN2 () {} is executed (){}.

Return to the duplicate name of the Javascript script function we just mentioned. For example, we define two functions funalert ():

Function Funalert ()
{
Alert ('A ');
}

Function Funalert ()
{
Alert ('B ');
}

Call funalert () to display a megbox with the content 'B '.

Why is the initialization function so effective? Here we only need to change the definitions of the above two functions to make it clear. We will change the definition:

VaR Fnalert =   New Function ( " Alert ('A ') " );
VaR Fnalert =   New Function ( " Alert ('B ') " );

Window. fnalert ();

the function is a function pointer defined on the object. We can execute the function pointer as if it was assigned a function reference, the duplicate name of the Script Function in Javascript is the same as the normal value assignment statement. It is equivalent to var I = 0 ;
var I = 1 ;

// Pay attention to the following JavascriptVaRUsing VaR to define variables is different from using advanced languages to define variables. It only serves as a prompt, reminding me that I have defined variables here, there is no definition of variable scope. As long as you do not leave the domain of the object that defines it (such as page refresh), the existing variables will always exist. Therefore, VaR can be written without writing. For example:If(True)
{
T= 100;
}
Alert (t );

Will display 100, while

If (True )
{
VaR T =   100 ;
}
Alert (t );

It also shows 100.

Therefore, Javascript script function names are unique.OperationThe problem is totally different from the syntax constraints in our advanced languages. Of course, it is not the scope of overload.

What is the use of duplicate Script Function names? The most intuitive way is to implementPseudo-OverloadFor example, if many of our free homepage spaces impose pop-up window advertisements on you, we can write the following on the first line of the page:

< Script Language = "JavaScript" >
VaR _ open = Window. open;
Window. Open= Function(){}
</Script>

In this way, the pop-up window advertisement that is not added to the first line of the page can be blocked (the first line cannot be blocked, because the window. open has not been 'reloaded '. Open is executed first ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.