The ' var ' in JScript defines the scope of variables _javascript tips

Source: Internet
Author: User
I don't remember when I looked at the grammar tutorial for JScript, which says it's perfectly legal to ignore the VAR keyword when declaring a variable. It was also because of the thought that JavaScript was loosely-typed language, so Var might really be a device. But it often turns out that the consequences of being taken for granted are unreliable.
Look at the results of the following examples to know the problem:
No.1
<script language= "JavaScript" > var var00 = 0; document.write (var00 + "); var Var01 = 1; function foo () {document.write (VAR01); var Var01 = 1; Foo (); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


No.2
<script language= "JavaScript" > var00 = 0; document.write (var00 + "); Var01 = 1; function foo () {document.write (VAR01); Var01 = 1; Foo (); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


No.3
<script language= "JavaScript" > var00 = 0; document.write (var00 + "); Var01 = 1; function foo () {document.write (VAR01); var Var01 = 1; Foo (); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


The implementation results of these three examples are:
Copy Code code as follows:

Results#region Results
No.1
0
Undefined

No.2
0
1

No.3
0
Undefined
#endregion


The original JavaScript variable is also scoped, but it is very general, it is divided into global variables and function variables. In the second example, we get 0 and 1 because all the variables are global variables, and that statement block defines two variables altogether. The first third, the global variable, does indicate that the VAR keyword has no relation. The var keyword within the function is critical, stating that the second Var01 is a variable within the function, so the output of the Var01 before initialization is ' undefined '.

So the function inside is to block out the var01 of the global? We know that you can use the following:: To access global variables, then javascript? In fact, we just need to understand what the global variable is, so we can get it. The original global variable is a property that is dynamically added to the instance window of the Window object, so we simply use: document.write (WINDOW.VAR01) in the function, and we can fetch the value 1. Also in this context, this in the function also points to the window instance, and we can write the reference as: This.var01.

    By the way,  when you re looking at the JScript tutorial, it says that the variable can only be [a-za-z_]+[a-za-z0-9_]* format, but ' $ ' It can also be used as a variable-name character, and can also be applied to the beginning, such as: $1234, and even:$$$  is also a valid variable name, faint.  

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.