Good Analysis of Variable-related details in JS _ javascript skills
Source: Internet
Author: User
A good analysis of Variable-related details in JS. Here I will discuss my understanding of the variable-related details in Javascript. If there are any mistakes, you are welcome to approve them.
I. Variable type
Javascript is different from Java and C. It is a non-type, weak detection language. Its definition of variables does not need to declare the variable type. We only need to assign values to the same variable by assigning values to various types of data. For example:
I = 100; // Number type
I = "variable"; // String type
I = {x: 4}; // Object type
I = [1, 2, 3]; // Array type
Although this feature of JS makes our encoding more flexible, it also brings about a disadvantage, which is not conducive to Debug. The weak detection of the compiler makes us quite painful to maintain lengthy code.
2. Variable Declaration
In JS, variable declarations are divided into explicit and implicit declarations.
Var I = 100; // explicitly declare
I = 100; // implicit statement
In a function, variables explicitly declared using the var keyword are used as local variables without the var keyword. global variables are declared using direct value assignment.
When we access an unspecified variable, JS reports an error. When we assign a value to a variable without declaration, JS will not report an error. On the contrary, it will think that we need to implicitly declare a global variable, which is 1.1 important.
3. Global and local variables
When the JS parser is executed, a global object is constructed in the execution environment. The Global attribute we define is read as the attribute of the object, in the top-level code, we can use the this keyword and the window object to access it. The local variables in the function body only exist in the called objects generated during function execution, and the local variables are destroyed immediately after the function is executed. Therefore, in program design, we need to consider how to reasonably declare variables, which not only reduces unnecessary memory overhead, at the same time, it can avoid repeated variable definitions and overwrite the debugging troubles caused by previously defined variables.
Iv. variable scope
The scope of variables in any programming language is a key detail. The scope of variables in JS is more free than that in JAVA and C. A major feature is that JS variables do not have block-level scopes and the variables in functions are valid throughout the function, run the following code:
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