Variables and function declaration advance questions

Source: Internet
Author: User

Experience Summary:

1, the declaration of the variable is advanced to the top of the scope, the assignment is retained in situ;

2, the function declaration is in advance;

3. When the function is assigned to a variable as a value, only the variable is "advanced" and the function is not "advanced".

As a best practice:

Variable declarations must be placed at the top of the scope/function (JavaScript has only function scope).

Example:

//The variable declaration is advanced to the top of the scope and the assignment remains in place. varA = ' out ';(functioninside () {alert (' Before: ' + a);//undefined     varA = ' in '; Alert (' After: ' + a);//' in '} ()) Alert (' Out: ' + a);//' Out '//equivalentvarA = ' out ';(functioninside () {varA//declaration in advanceAlert (' Before: ' + a);//A is not initialized, so it is undefinedA = ' in '; Alert (' After: ' + a);//A is assigned a value of ' in '} ()) Alert (' Out: ' + a);
//the function declaration is in advance//function declaration format: Functions fun () {...}Fun ();//' Hello,world. 'functionFun () {alert (' Hello,world. '));  }fun (); //' Hello,world. '//when the function is assigned to a variable as a value, only the variable is advanced and the function is not advancedFun ();//TypeError error, stating that fun is not a functionvarFun =function() {alert (' Hello,world. '));  };fun (); //' Hello,world. 'equivalent to:varFun;fun (); //Fun hasn't been initialized yet, so it's not a functionFun =function() {alert (' Hello,world. '));  };fun (); //' Hello,world. '

The specific details of this knowledge point is probably so much, seriously think about experience.

Reference:

The topic of knowledge

Blog 1

Some official documentation

Variables and function declaration advance questions

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.