Functional declarations and variable declarations in JavaScript

Source: Internet
Author: User

Observe the following two sections of code and try to write the result of hello (' word '):

// variable-type declaration function Hello (msg) {   alert (msg);    var function (){};   Alert (msg);} // Functional Declarations function Hello (msg) {   alert (msg);    function msg () {};   Alert (msg);}

For a variable declaration, the first pop-up word, and then the function, and the functional declaration, two times is the popup function. Why is that?

One: Functional declaration

functionHello (msg) {alert (msg);//print msg in the front position, is function   functionmsg () {//This msg is a functional declaration that runs through the scope of the whole fun function      //that is, as soon as fun is called, even as long as it enters the parsing stage      //then the MSG function will be parsed first, and you can think of it as a fun function      //the first variable inside. Some books call this phenomenon a statement being advanced or promoted. } alert (msg);//Print msg at the very end of the position, also function  //Description Functional declarations are run through within the Hello scope}

Two: variable declaration

functionHello (msg) {alert (ABC);//This is undefined   varABC =function(){    //This is a variable declaration, and its position within the fun function is fixed, which means    //before var abc, the value of ABC is undefined within the fun    //after VAR ABC, ABC is the value it declares.    //this way you can understand that a var abc is written in the first line of the fun function;    //because of this, the variables declared in the fun will cut off the references to the external variables;    //regardless of the hello scope, what is the value of ABC, inside Hello, before var abc, is always undefined    //This is one of the interviewers ' favorite places to visit.} alert (ABC); //This is the function.

This is the same thing as ABC for MSG. For the inside of Hello, its formal parameter is equivalent to the local variable inside the hello.
The intention here is to use ABC to replace MSG, in fact, to simplify the problem and reduce the disturbance. Since it is the local variable within the Hello, then before the declaration
Its value is undefined, which is a matter of course. The only thing to notice here is that the declaration of a formal parameter appears before all local variables.
After repeating the local variable declaration, the system ignores the repeated declaration process, except that the declaration is assigned the same value.
}

To summarize: A functional declaration rises to the front of the owning scope, and the variable declaration does not. Finally, add one point:
var function (){};
ABC actually holds a reference to a nameless function, and ABC is essentially a variable.    and function abc () {} is a well-known function; ABC is essentially a function; If you like, you can save a reference to the same function with multiple variables:
var var bbb = ABC;

References can be countless, but the real thing is always unique.

xxx = Null,bbb=null, what should ABC do, and var abc = function () {};   ABC = NULL after you have completely lost everything about ABC.   ABC from now on, from the lake, Incognito, disappeared ...  Although the two are functionally equivalent, but, I personally still tend to the manipulation of the function, artificial written in front, this is to avoid the compression of the merger, resulting in a variety of unexpected results. If it's not a generic method, I'm used to declaring it in variable style, which is, of course, a personal habit. For a functional declaration, it is visible throughout the scope, and some of the books say that it is a declaration of ascension, or a declaration in advance. In fact, what is called unimportant, understand what happens to this phenomenon, what impact is the fundamental. Finally, it is important to emphasize that the name of a named function expression is valid only within the scope of the function. That is, in the Hello function, regardless of the way the function is declared MSG, then the Hello function, there is no.

Functional declarations and variable declarations in JavaScript

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.