Measure the test taker's knowledge about the method of initializing JavaScript Functions.

Source: Internet
Author: User
Tags variable scope
From the perspective of JavaScript function name duplication, we have a buddy who asked me what would happen if the JavaScript function was renamed again today? I didn't think about it at first, so I thought it might be wrong. But after my experiment is finished, I find that there is no Script Error prompt on the page, and the program is running, only the function with the same name is called.

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 ();Its function is a function pointer defined on the object. We get the function reference assigned by this pointer and it will execute any effect, the Script Function in JavaScript has the same name as the normal value assignment statement, which is equivalent: Var I = 0 ;
Var I = 1 ;// Pay attention to the following JavaScript VarUsing 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(){}
>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.