From the name of JavaScript's function to see its initialization way _javascript skill

Source: Internet
Author: User
Tags variable scope
Today there is a Dude Ask me what happens if the JavaScript function has the same name? At first I did not think carefully, said that may be wrong, but when I finished the experiment found that the page does not have any script error prompts, and the program is running, but 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 ();
The function is defined as a function pointer over the object, and what effect does it perform as a function reference to the pointer, and the name of the script function in JavaScript is the same as a normal assignment statement, equivalent to:
var i = 0;
var i = 1;
A little attention to the following JavaScript var, defining variables with Var is different from the high-level language definition variables we use. It only acts as a hint, reminding me that I define the variable here, and that there is no variable scope concept, as long as the variables that have been present will persist without leaving the domain of the object that defines it (such as page refreshes). So var can write without writing. As an example:
If ( true )
{
T = m ;
}
Alert (t);

will show 100, while

If ( true )
{
var t = m ;
}
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).
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.