Home  >  Article  >  Web Front-end  >  Different understandings and analysis of JavaScript variable declaration and definition of variable var

Different understandings and analysis of JavaScript variable declaration and definition of variable var

伊谢尔伦
伊谢尔伦Original
2017-07-18 10:57:261426browse

Let’s look at a simple example to illustrate the problem

if (!("a" in window)) {
    var a = 1;
}
alert(a);

First of all, all global variables are attributes of window. The statement var a = 1; is equivalent to window.a = 1;

You can use the following method to detect whether global variables are declared

"Variable name" in window

Second, all variable declarations are in scope At the top of the scope, look at a similar example:

Copy code The code is as follows:

alert("a" in window);
var a;

At this time, although the statement is after alert, alert pops up is still true, this is because the JavaScript engine will first sweep all variable declarations, and then move these variable declarations to the top. The final code effect is like this:

var a;
alert("a" in window);

Third, you need to understand the question What it means is that the variable declaration is advanced, but the variable assignment is not, because this line of code includes both variable declaration and variable assignment.

You can split the statement into the following code:

var a;    //声明
a = 1;    //初始化赋值

So to sum up, when variable declaration and assignment are used together, the JavaScript engine will automatically divide it into two In order to declare the variable in advance, the assignment step is not advanced because it may affect the execution of the code and produce unpredictable results.

The code in the question is equivalent to:

var a;
if (!("a" in window)) {
    a = 1;
}
alert(a);

According to the analysis of the above example question, when declaring a variable, if it is a declared local variable, var must be added in front, if it is a global variable, You don’t need to add var (it’s best to limit the number of global variables and try to use local variables)

The following is a description of several features of using var

Use var statements more Not only is it legal to declare a variable twice, it also won't cause any errors.
If a repeatedly used statement has an initial value, then it only plays the role of an assignment statement.
If a reused statement does not have an initial value, it will not have any impact on the original variables.
Variables without var declaration exist as global variables; variables with var declaration are local variables, especially inside functions. Moreover, after testing, declaration with var is faster than without var. Set up as many local variables as possible in the function, so that it is safe and fast, and the variable operations are more reasonable. Logic errors will not be caused by random manipulation of global variables in the function.

When declaring an object, it is best to use the object's self-face method, which is much faster than the new method.

The variable name is chosen by yourself. In order to take care of semantics and specifications, the variable name may be slightly longer, but please note that the length of the variable name will also affect the execution speed of the code. Declarations with long variable names do not execute as quickly as short ones.

The title is as follows. The question is: What are the results of the two alerts?

<script
type="text/javascript">
    var a = 1;
    var a;
    alert(typeof a);
(function () {
        b = &#39;-----&#39;;
        var b;        
    })();
    alert( typeof b);
</script>

Run the code in Chrome, and the correct result of the code is 1.number 2.undefined. What is examined here is the concept of JavaScript variable declaration in advance.

We are looking at another example, such as the following:

test();
function test(){    
alert("Hello World!");
}

The program will not report an error, but the running result is: Hello World!. Principle: Before the computer starts executing the statement, it will first search for all function definitions and then save the related functions.
Question 1:
var a = 1;
var a;
Declaring variable a on line 2 is equivalent to declaring a at the top, and then the first sentence is to redeclare a, and then Assign a value of 1. So typeof a is number
Question 2:
b = '-----';
var b;
Analysis of the second question: b='-----', program First, it will check whether there is a declaration of variable b in the context. If so, it will directly assign the value to '-----'. But alert(typeof b); is outside the function and outputs the global variable b, all of which are undefined.
Please note: The assignment operation to variables is not advanced.

The above is the detailed content of Different understandings and analysis of JavaScript variable declaration and definition of variable var. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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