JavaScript Variable Declaration instance Analysis
This article mainly introduced the JavaScript variable declaration, the example analyzes the JavaScript variable declaration related use skill, needs the friend to be possible to refer to under
This example describes the method for JavaScript variable declarations. Share to everyone for your reference. The specific analysis is as follows:
JS in the use of a variable should be declared first. Variables are declared using the keyword var.
If the variable is not given an initial value in the Var declaration statement, the variable value is undefined.
Instead of specifying a variable type when declaring a variable, the JS variable can be any data type.
It is legal and harmless to use the VAR statement to repeat the declaration of a variable. If you have a duplicate declaration with an initializer, it is no different from a simple assignment statement.
If you attempt to read a variable that is not declared, JS will complain. Under the ECMAScript5 strict mode, assigning values to a variable that is not declared can also be an error; Historically, in a strictly restrictive mode, if you assign a value to an undeclared variable, JS actually creates a property of the same name for the global object, and it seems to work like a properly declared global variable. This means you'll be lucky not to declare global variables, but this is a bad habit that can cause a lot of bugs, and it's best to always use VAR to declare variables.
In a function body, a local variable with the same name overrides the global variable.
Although global scope write code may not write var statements, you must use the VAR statement when declaring local variables, referring to the following code:
?
1 2 3 4 5 |
Scope = "global"; function foo () {scope= "local"//fk! We've just modified the global variable!!! } |
In a programming language similar to the C language, each piece of code in the curly braces has its own scope, and the variables are not visible beyond the declaration of their code snippet, which we call block-level scopes, while JS does not have block-level scopes, but instead uses functional scopes (function SCOPE): Variables are defined in the body of the function that declares them and in any function that is nested within the function body (whether nested or outer nesting?)
The function scope of JS is that all variables declared within a function are always visible in the body of the function, which means that the variable can be used even before the declaration is declared. This feature of JS is informally called Declaration advance (hoisting), that is, all variables declared in the JS function (but not assigned) are "advanced" to the top of the function body.
?
1 2 3 4 5 6 7 8 |
var scope = "global"; function f () {Console.log (scope);//output "undefined" instead of "global" var scope = "local";//The variable is assigned an initial value here, but the variable is defined anywhere in the function body. Console.log (scope); Output "local" |
The above code is equivalent to:
?
1 2 3 4 5 6 |
function f () {var scope; Console.log (scope); scope = "local"; Console.log (scope);} |
When a JS global variable is declared, a property of the global object is actually defined.
When you declare a variable with VAR, this property is not configurable and cannot be deleted with the delete operator, but when you do not use strict mode and assign a value to an undeclared variable, JS automatically creates a global variable, and the variable created in this way is the normal configurable property of the global object. Can be deleted:
?
1 2 3 4 5 6 |
var x = 1; y = 2; This.z = 3; Ditto delete x; Returns FALSE, the variable delete y cannot be deleted; Returns TRUE, the variable is deleted delete this.z//Ibid. |
I hope this article will help you with your JavaScript programming.