Home  >  Article  >  Web Front-end  >  A brief analysis of JavaScript variable declaration_javascript skills

A brief analysis of JavaScript variable declaration_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:24:131201browse

No matter where a JavaScript variable declaration statement appears, it will be executed first before other code. The scope of a variable declared using the var keyword is the current execution context, which may be a peripheral function, or, when the variable is declared outside the function body, a global variable.

Things defined outside the function body are global variables, and those defined inside the function body are local variables. The definition here refers to the declaration through var.

JavaScript has the concept of implicit globals, which means that any variable you do not declare will become a global object property. For example:

function test(){
myname = "huming";
alert(myname);
}
test();  // "huming"
alert(myname);  //"huming" 

The two results are the same, indicating that myname is a global variable.

So, is there any difference between implicit global variables and explicitly defined global variables? . The answer is definitely yes, look at the example below:

// 定义三个全局变量
var global_test = ;
global_test = ; // 反面教材
(function () {
global_test = ; // 反面教材
}());
// 试图删除
delete global_test; // false
delete global_test; // true
delete global_test; // true
// 测试该删除
alert(typeof global_test); // "number"
alert(typeof global_test); // "undefined"
alert(typeof global_test); // "undefined" 

As can be seen from the above example: global_test1 defined by var outside the function cannot be deleted, and global_test2 and global_test3 that are not defined by var are deleted (regardless of whether they are created within the function body).

In summary, global variables declared through var outside the function body cannot be deleted, but implicit global variables can be deleted.

Note here: JavaScript has a behavior called "hoisting" (suspending/top parsing/pre-parsing).

Let’s illustrate with an example:

var myname = "huming"; //声明全局变量
function test() {
alert(myname);
var myname = "local_huming";
alert(myname);
}
test();

Do you think the contents of the two alerts are consistent? ? Obviously inconsistent, needless to say consistent. . Actual output is: "undefined", "local_huming".

The above example is equivalent to

var myname = "huming"; //声明全局变量
function test() {
  var myname;
  alert(maname);<br>  myname = "local_huming";
  alert(myname); // "local"
}
test(); 

The myname output by the first alert is not the global variable you think, but a local variable in the same scope (a function body) with it. Although it has not been declared, it is treated as such. This is called "hoisting".

This should make it clear. When you use a variable in a function body and then redeclare it later, an error may occur.

Writing specifications:

function test() {
var a = ,
b = ,
c = a + b,
d = {},
e,
f;
// function body...
}

The benefits are:

1. All local variables are defined at the beginning of the function for easy search;

2. Prevent logical errors caused by using variables before they are defined.

In JavaScript, there are four ways for a variable name to enter the scope

Language built-in, all scopes have this and arguments keywords

Formal parameters, function parameters are valid in the entire scope

Function declaration

Variable declaration

The four orders listed above are also the order of priority from high to low. Once a variable name has been declared, it cannot be overwritten by other lower priority variable declaration forms.

Statement:
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