JavaScript is a product that follows the ECMAScript standard, the standard of natural ECMAScript to follow.
Let's take a look at the definition and usage of the VAR keyword
The Var statement is used to declare a variable.
The creation of a JavaScript variable is also called a "declaration" variable:
Copy Code code as follows:
Variable is empty (no value) after the variable declaration.
To copy the variable, the operation is as follows:
Copy Code code as follows:
When declaring a variable, you can also assign a value to a variable:
Copy Code code as follows:
Grammar
Copy Code code as follows:
Parameter values
Parameters |
Description |
VarName |
Have to. Specifies the variable name. Variable names can contain letters, numbers, underscores, and dollar signs.
- Variable names must begin with a letter
- Variable names can also start with $ and _ (but not generally)
- Variable names are case sensitive (Y and y are different variables)
- Reserved words (such as JavaScript keywords) cannot be used as variable names
|
Value |
Optional. Specifies the value of the variable.
Note: If the variable declaration does not specify a value, the default value is undefined |
We all read a lot of articles, all said to avoid the implicit declaration of global variables, that is, declare variables must add ' var ', that added ' var ' and not ' var ' What is the difference?
Let's look at a piece of code
var a = ' AA ';
alert (a); Pop ' AA '
See, you declare a global variable that actually adds an attribute to the ' window ' object, and the following code has the same effect.
A = ' AA ';
alert (a); Pop ' AA '
That "var a = ' AA '" and "a = ' AA '" are all global variables, what's the difference? Look at the following two paragraphs of code
var a = ' AA ';
Delete WINDOW.A; False
a = ' AA ';
Are all added attributes for the ' window ' object, one that can be deleted and one that cannot be deleted. But Plus ' var ' can be scoped, without ' var ' always adding properties to the ' Window ' object dynamically, the following code is proof
var test = function () {
a = ' AA ';
}
Test ();
Because the Window object is a global object, the default can be no, the same effect as the following
var test = function () {
a = ' AA ';
}
Test ();
Speaking of this, serious thinking of the classmate now must have a question, why the implicit declaration of global variables can be deleted, explicitly declared global variables cannot be deleted?
The reason is that "delete cannot remove those properties that are configurable to false", and some built-in object properties are not configurable, such as the properties of global objects created by variable declarations or function declarations, and the following code is evidence
Delete Object.prototype; False can not be deleted, this property is not configurable
var a = ' AA ';
Delete Window.a;//false cannot be deleted, which is not configurable
function test () {};
That doesn't make sense, the global variable, declared by ' var ', is actually adding a configurable property to the ' Window ' object instead of the ' var ' declared global variable, which actually adds a configurable attribute for the ' window ' object.
Notice that the window can be replaced with this by using the window, such as:
var test = function () {
a = ' AA ';
}
Test ();
For the reason, please look at the article I wrote earlier ' This,this, discuss this in JavaScript again, Super comprehensive '
Below, pull out the Var keyword in JavaScript and give you a separate explanation.
We know that when we define variables, we need to use the var keyword, and when we use the var keyword, we need to pay attention to how he uses it:
The following columns fully demonstrate that Var has different execution results when used and not used, global variables, and local variable definitions.
var Var01 = 1;
function Funtest () {
document.write (VAR01);
var Var01 = 0;
The result is: undefined
var Var01 = 1;
function Funtest () {
document.write (VAR01);
Var01 = 0;
Results are: 1
Var01 = 1;
function Funtest () {
document.write (VAR01);
var Var01 = 0;
}
The result is: undefined
Var01 = 1;
function Funtest () {
document.write (VAR01);
Var01 = 0;
}
Results are: 1
See here on the JavaScript of the Var you know how much, I believe that we learn more or less through this article will be a little harvest it. To learn more about JavaScript var related knowledge Please continue to focus on this site, thank you!