Home > Web Front-end > JS Tutorial > Instructions for using variables in Javascript_Basic knowledge

Instructions for using variables in Javascript_Basic knowledge

WBOY
Release: 2016-05-16 18:27:04
Original
1133 people have browsed it

All variables in JavaScript are loosely typed. Unlike other object-oriented languages, variable declarations are strongly typed. Therefore, variable declarations in Javascript do not include types. Declare a variable through the var keyword or directly write the variable name, such as:
var v = 1;
v=1;

At this time, someone may ask, what are the above two declarations? What is the difference and why are there two different declaration methods? This involves the scope of variables in JavaScript. In JavaScript, the scope of variables includes global and function level.

Global variables can be declared outside the function. No matter which of the above declaration methods is used, variables declared outside the function are global variables. For example:

Copy code The code is as follows:



Run results: 1 2

In addition, if the variables declared inside the function do not use the var keyword, the declared variables will also be global variables. For example:
Copy code The code is as follows:



Run result: 1
However, it should be noted that in this case, if you want to use a variable, you must first call the function that declares the variable to initialize the variable, such as foo( ), otherwise, a "variable v is undefined" error will occur.

Global variables will exist as properties of the window object because they can be accessed through window.$ ($ represents the variable name). Of course, it can also be accessed directly through the variable name. The following will talk about why there are these two access methods.

Variables declared with the var keyword inside a function will be function-level variables, and their scope is limited to the inside of the function. For example:
Copy code The code is as follows:



Run result: 1 Variable "v" is undefined

Through the above analysis, it can be found that the main function of the keyword var is to define function-level variables .

Careful friends may ask, what will be the result if the same variables are defined inside and outside the function? For example:
Copy code The code is as follows:



Run result: undefined
! ! ! ! ! Some people may be depressed. v is clearly defined outside the function foo(). Why is it undefined? This involves the parsing of javascript. According to experience, JavaScript’s parsing process for variables in a function body is:
Search for all var keywords, put their variable declarations at the front of the function body, and the assignment and use remain unchanged. In this way, the above JavaScript actually is equivalent to:
Copy code The code is as follows:



According to this analysis, it is obvious that the above results are produced, due to the priority of the variables inside the function Higher than the priority of global variables (this is the case in most programming languages), the variable v inside the function overrides the global variable v, but since the variable v inside the function is used, it is only declared but not assigned, so the result is undefined.

If you still need to use the defined global variable v in the method body, the window object comes in handy at this time and can be accessed through window.v. For example:
Copy code The code is as follows:



运行结果:2 undefined
Related labels:
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