JavaScript Variable _ javascript skills

Source: Internet
Author: User
JavaScript variable declaration statements are first executed before other code no matter where they appear. This article describes how to declare javascript variables, if you are interested, you can learn about the JavaScript variable declaration statement, which is first executed before other code no matter where it appears. Using the var keyword to declare the scope of a variable is the current execution context, which may be a peripheral function, or when the variable declaration is outside the function body, it is a global variable.

Global variables are defined in the function body and local variables are defined in the function body. The definition here is declared through var.

JavaScript has an implicit global concept, which means that any variable you do not declare will become a global object attribute. 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.

There is no difference between an implicit global variable and a clearly defined global variable .. The answer is yes. Let's look at the example below:

// Define the three global variables var global_test =; // The opposite textbook (function () {global_test =; // The opposite textbook }()); // try to delete global_test; // falsedelete global_test; // truedelete global_test; // true // test the deletion of alert (typeof global_test ); // "number" alert (typeof global_test); // "undefined" alert (typeof global_test); // "undefined"

As shown in the preceding example, global_test1 defined by var cannot be deleted, global_test2 and global_test3, which have not been defined by var, are deleted (whether they are created in the function body or not ).

In summary, the global variables declared through var in the function in vitro cannot be deleted, while the implicit global variables can be deleted.

Note: JavaScript has a behavior called "hoisting" (mounting, top resolution, and pre-resolution ).

Here is an example:

Var myname = "huming"; // declare the global variable function test () {alert (myname); var myname = "local_huming"; alert (myname);} test ();

Do you guess the contents of alert are the same twice ?? Obviously, they are inconsistent. Do you still need to say they are consistent .. The actual output is: "undefined", "local_huming ".

The above example is equivalent

Var myname = "huming"; // declare the global variable function test () {var myname; alert (maname );
Myname = "local_huming"; alert (myname); // "local"} test ();

The myname output by alert for the first time is not a global variable, but a local variable in the same scope (a function body) as it. Although it has not been declared, it is considered as a declaration. This is the so-called "hoisting ".

This should be clear. If you use a variable in the function body and then declare it again, an error may occur.

Writing specifications:

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

Benefits:

1. All local variables are defined at the beginning of the function to facilitate searching;

2. prevent logical errors used before variables are defined.

In javascript, a variable name can enter the scope in four ways.

Language built-in. All scopes contain the this and arguments keywords.

Form parameters, function parameters are valid throughout the scope

Function Declaration

Variable Declaration

The four sequences listed above are exactly the order of high-to-bottom priorities. Once a variable name has been declared, it cannot be overwritten by other lower-priority variable declaration forms.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.