Do you want to declare variables in JS?

Source: Internet
Author: User

Hello, JS language is a weakly typed language that can be used directly without a declaration, and is used as a global variable by default.
    
Suggestions:
In function, the var declaration variable should be used so that the amount of change only exists during the lifetime of the function and will not contaminate the global control. As for the use of the <script> tag directly, it is stated that the effect is the same.

The following article can help you understand the JS variable more clearly
I. Types of variables
JavaScript, unlike Java and C, is a non-type, weak-detection language. It does not need to declare variable types, and we can assign various types of data to the same variable in the form of an assignment. For example:

I=100;//number type
i= "variable";//string type
I={x:4};//object type
I=[1,2,3];//array type

This feature of JS makes our coding more flexible, but also brings a disadvantage, not conducive to debug, the compiler's weak detection allows us to maintain the lengthy code is quite painful.

II. Declaration of variables

JS in the variable declaration of the explicit Declaration and implicit declaration.

VAR i=100;//Explicit declaration

i=100;//Implicit declaration

Using in a functionvar keywordthe variable that is explicitly declared is done asLocal Variables,instead of using the VAR keyword, a global variable is declared with a direct assignment.
 when we use access to a non-declared variable, JS will error.   And when we assign a value to a variable that is not declared, JS does not give an error, but instead it thinks we are going to implicitly declare a global variable, which we must pay attention to.


Third, global variables and local variables
When the JS parser executes, it first constructs a global object in the execution environment, and the global attribute we define is read as the property of the object, and in the top-level code we can access it using the This keyword and the window object. The local variables in the function body only exist in the call object generated when the function executes, and the local variables are destroyed immediately when the function is completed. Therefore, in the program design we need to consider how to reasonably declare variables, so as to reduce unnecessary memory overhead, but also to a large extent to avoid duplicate definitions of variables to overwrite the previously defined variables caused by the debug trouble.
Iv. Scope of variables
The scope of variables in any programming language is a critical detail. JS in the scope of the variables relative to Java, C and other languages appear freer, a big feature is that the JS variable has no block-level scope, the function of the variables in the entire function is valid, run the following code:

<script language= "JavaScript" type= "Text/javascript" >
Define an output function
function OutPut (s) {
Document.writeln (s)
}
Global variables
var i=0;
Defining external functions
function outer () {
Accessing global variables
OutPut (i); 0
Define a class-part function
function inner () {
Defining local Variables
var i = 1;
I=1; If implicit declaration is used, then the global variable i is overwritten
OutPut (i); 1
}
Inner ();
OutPut (i); 0
}
Outer ();
</SCRIPT>
The output is 0 1 0, from the above can be proved that JS if you declare a variable in the function body with Var, then this variable is only valid in the body of the function, when the function ends, the local variable can be destroyed.
Because of this JS feature above, there is a key issue to note. Previous use of ActionScript, although it and JS are based on the ECMA standard, but here is still slightly different. For example, the following code:

<script language= "JavaScript" type= "Text/javascript" >
Define an output function
function OutPut (s) {
Document.writeln (s)
}
Global variables
var i=0;
Defining external functions
function outer () {
Accessing global variables
OutPut (i); 0
Define a class-part function
function inner () {
OutPut (i); Undefiend
var I=1;
OutPut (i); 1
}
Inner ();
OutPut (i); 0
}
Outer ();
</SCRIPT>
You may think that the output is 0 0 1 0, in fact, in the as is true, and the input in JS is 0 undefined 1 0, why this? Just now we mentioned that the local variables declared in the JS function body are valid throughout the function, so var i = 1 in the code above, and in the inner function, in fact, the explicitly declared variable i is compiled into the calling object at precompiled time, unlike an implicitly declared variable that is defined as a global variable at the time of interpretation. , only when the output (i) is called, the variable is not initialized, and the local variable i is an unassigned variable, not an undefined variable, thus outputting the undefined. The above code is equivalent to the following code:

function inner () {
var i; Define but not assign a value
OutPut (i); Undefiend
I=1;
OutPut (i); 1
}
To avoid this type of problem, it is highly recommended to make a function declaration in the start-of-function set.
V. Basic types and reference types
JS is different from Java, C these languages, when the variable declaration does not need to declare the variable storage space. The data stored in a variable can be divided into two categories: the base type and the reference type. Where numeric, Boolean, NULL, and undefined are basic types, objects, arrays, and functions belong to reference types.
The base type has a fixed memory size in memory. For example: A numeric type occupies eight bytes in memory, and a Boolean value occupies only one byte. For reference data, they can have any length, so their memory size is variable, so the variables stored in is actually a reference to this data, usually memory address or pointer, through which we can find this data.
Reference types and basic types also differ in usage behavior:

<script language= "JavaScript" type= "Text/javascript" >
Define an output function
function OutPut (s) {
Document.writeln (s)
}
var a = 3;
var B = A;
OutPut (b);
3
A = 4;
OutPut (a);
4
OutPut (b);
3
</SCRIPT>
When you assign a basic type B, it actually opens up a memory space, so changing the value of variable A has no effect on variable B.

<script language= "JavaScript" type= "Text/javascript" >
Define an output function
function OutPut (s) {
Document.writeln (s)
}
var = [A_array];
var b_array = A_array;
OutPut (B_array); The
A_ARRAY[3] = 4;
OutPut (B_array);//1,2,3,4
</SCRIPT>

The above is a variable assignment to a reference type, in fact they pass a reference to the memory address, so access to A_array and B_array is, in fact, the same piece of memory area of the operation. If you want to reallocate memory space to store reference variables, then I need to use a clone method or a custom method to copy the data of the reference variable.

Source: >  

From for notes (Wiz)

Do you want to declare variables in JS?

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.