Home > Web Front-end > JS Tutorial > body text

Introduction to variable definition and storage in JavaScript_javascript skills

WBOY
Release: 2016-05-16 16:22:59
Original
1144 people have browsed it

Unlike programming languages ​​such as C and Java, variables in JavaScript are untyped, and all variable definitions use the keyword var:


Copy code The code is as follows:

var a;
var m, n;
var x=42, y="test";


If a variable is not assigned a value after it is defined, the value of the variable is undefined. For example, the values ​​of the three variables a, m, and n in the above code are all undefined.

Since variables in JS are typeless, it is completely possible to assign different types of values ​​to the same variable, such as:


Copy code The code is as follows:

var b = "temp";
console.log(typeof b);//string
b = 108;
console.log(typeof b);//number


In addition to different types of assignments to the same variable, JavaScript can also define variables repeatedly; if you do this, the variable definition statement after the first time is equivalent to the assignment statement:


Copy code The code is as follows:

var c = "hello";
console.log(c);//hello
var c = true;
console.log(c);//true


In the strict mode of the ECMAScript standard, all variable definitions must use the var keyword. If strict mode is not used, when a JS program assigns a value to an undefined variable, the program will create a property with the same name as the variable in the JS global object, that is, a new global variable will be created. This approach will cause many problems (for example, global variable pollution between multiple JS programs, etc.) and will bring a lot of trouble to later maintenance; therefore, in the actual development process, this approach should be avoided as much as possible.

Storage of variables

If the defined variable is a global variable and the var keyword is not used in the variable definition process, then the variable will exist as an attribute of the global object, which can be obtained by accessing the corresponding attribute of this (global object), or It can be deleted from the global object by using the delete keyword:


Copy code The code is as follows:

var e = "globalVariableValue";//defined outside of any function, it is a global variable, but does not store in "this"
f = "globalVariableValue2";
this.g = "globalVariableValue3";
console.log(this.e);//undefined
console.log(this.f);//globalVariableValue2
console.log(this.g);//globalVariableValue3

delete f;
delete g;
console.log(this.f);//undefined
console.log(this.g);//undefined


For each function call in JavaScript, JavaScript will create a local object to store the local variables defined in the function; if there is a nested function defined inside the function, JavaScript will create a local object after the function has been defined. Define a nested local object inside the local object. For a function, there are as many layers of nested local objects as there are layers of nested function definitions inside it. This local object is called a "function call object" ("call object" in ECMAScript 3, renamed "declarative environment record" in ECMAScript 5, but personally I think the name in ECMAScript 3 is easier to understand).

Contrary to the global object this, JavaScript does not provide any way to access these local objects (function call objects). Therefore, developers cannot operate on these local objects. However, understanding these function call objects will be of great help in understanding some concepts in JavaScript, such as variable scope and closures.

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