Home > Web Front-end > JS Tutorial > Detailed explanation of JavaScript variable declaration_javascript skills

Detailed explanation of JavaScript variable declaration_javascript skills

WBOY
Release: 2016-05-16 16:30:09
Original
1457 people have browsed it

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:

Copy code The code is as follows:

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:

Copy code The code is as follows:

// Define three global variables
var global_test1 = 1;
global_test2 = 2; // Negative teaching material
(function () {
Global_test3 = 3; // Negative teaching material
}());
// Attempt to delete
delete global_test1; // false
delete global_test2; // true
delete global_test3; // true
// Test the deletion
alert(typeof global_test1); // "number"
alert(typeof global_test2); // "undefined"
alert(typeof global_test3); // "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 cannot be deleted, but implicit global variables can be deleted.

It should be noted here: JavaScript has a behavior called "hoisting" (suspending/top parsing/pre-parsing).

Let’s illustrate with an example:

Copy code The code is as follows:

var myname = "huming"; //Declare global variables
function test() {
alert(myname);
var myname = "local_huming";
alert(myname);
}
test();

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

The above example is equivalent to

Copy code The code is as follows:

var myname = "huming"; //Declare global variables
function test() {
var myname;
alert(maname);
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 redeclare it later, an error may occur.

Writing specifications:

Copy code The code is as follows:

function test() {
var a = 1,
b = 2,
        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 when variables are used before they are defined.

Have you understood the variable declaration of JavaScript? The above content is very detailed and easy to understand. The final summary is also very pertinent. Don’t miss it.

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