JavaScript scopes: The scope of JavaScript

Source: Internet
Author: User
Tags variable
 
 
  1. /***************************************
  2. * The result is to print out the global
  3. ****************************************/
  4. var a = ' global ';
  5. (Test () {
  6. Write (a);
  7. })();
  8. /***************************************
  9. * The result is to print out the undefined
  10. ****************************************/
  11. var a = ' global ';
  12. (Test () {
  13. Write (a);
  14. var a = ' local ';
  15. })();
  16. /***************************************
  17. * The result is to print out the local global
  18. ****************************************/
  19. var a = ' global ';
  20. (Test () {
  21. var a = ' local ';
  22. Write (a + ' \ n ');
  23. })();
  24. Write (a + ' \ n ');


As can be seen from the above example, the scope of JS is a function body. When a function body defines a local variable with the same name as a global variable, the global variable is overwritten by the local variable.

This article links http://www.cxybl.com/html/wyzz/JavaScript_Ajax/20130318/37230.html

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.