Home > Web Front-end > JS Tutorial > 'var' in JScript defines the scope of variables_javascript tips

'var' in JScript defines the scope of variables_javascript tips

WBOY
Release: 2016-05-16 19:17:42
Original
1548 people have browsed it

I don’t even remember when I read the JScript syntax tutorial. It said that it is completely legal to ignore the var keyword when declaring variables. At that time, I also felt that JavaScript was a loosely-typed language, so var might really be just a decoration. But facts often prove that the taken-for-granted results are unreliable.
Look at the results of the following examples and you will know the problem:
No.1


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

No.2

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]

No.3 [Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]
These three The execution results of the example are: Copy the code

The code is as follows:


Results#region Results
No.1 

undefined 

No.2 



No.3 
undefined 
#endregion



It turns out that JavaScript variables also have scopes, but they are very general and are divided into global variables and function variables. In the second example, you get 0 and 1 because all variables are global variables, and that statement block defines a total of two variables. The first and third global variables outside the function really mean that it does not matter whether the var keyword is present or not. The var keyword within the function is very critical. It indicates that the second var01 is a variable within the function, so the output before initializing var01 is naturally 'undefined'.
So is the global var01 blocked in the function? We know that in C/C you can use :: to access global variables, but can it be done in JavaScript? In fact, as long as we understand what global variables are, it will be easy. It turns out that global variables are attributes dynamically added to the window instance of the Window object, so we only need to use: document.write(window.var01); within the function to get its value 1. At the same time, in this context, this in the function is also the window instance pointed to. We can also write the reference as: this.var01. By the way, when I re-read the JScript tutorial, it said that variables can only be in the format of [a-zA-Z_] [a-zA-Z0-9_]*, but '$' can also be used as a variable name character, and can also be used at the beginning, such as: $1234, and even: $$$ is also a legal variable name, faint.​
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template