"Go" javascript the difference between Var and no VAR for global variables

Source: Internet
Author: User

I believe you are not unfamiliar with the global variables, in the function scope of the form defined by A=1 is a global variable, in the global scope, in the following 3 forms can be created to the globally visible name:

1 <script>2var a = 1; 3 b = 2; 4 window.c = 3; 5 </script>

For b=2 this way, it is actually the same as C, in the execution of this assignment, we will find the variable named B along the scope chain, always find the top of the scope chain has not been found, and then add a property B to the window and then assign a value.

There are two differences between Var and no Var:

A global variable of 1 var cannot be deleted because delete intelligently deletes the deleted property of the object, while the global attribute defined by VAR is marked as non-removable. It should be stated that the delete is unsuccessful and does not throw a wrong, and the return value of delete is True|false.

The global variables defined by the 2 Var are promoted, without the global variables defined by VAR being promoted. You can see the results of the following programs:

<script>alert (a); var a=1; </script>
<script>alert (a); // error, a undefineda=1; </script>

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.