Learn JavaScript three (variable) from scratch

Source: Internet
Author: User

One, variable

ECMAScript variables are loose variables, so-called loose variables, that is, variable names can hold any type of data, each variable is just a placeholder for holding the value.

To use the var operator when defining variables

Such as:

var message; /* defines a variable named message that can be used to hold any value, and the uninitialized variable will hold a special value-undefined */
second, local variables

You can declare a variable and initialize it simultaneously.

function Test () {    var message= ' Hello ';  // defines a local message variable with an initial value of Hello    alert (message);       // Pop the message content     }    Test (); </script>

The wrong wording

function Test () {    var message= ' Hello ';  // defines a local message variable with an initial value of Hello         }    Test (); alert (message);        // Error

As can be seen from the above two examples, if you use var to define a variable in a function, the variable will be destroyed after the function exits.

third, define global variables
function Test () {   message= ' Hello ';  // Global message variable with an initial value of Hello         }    Test (); alert (message);        // Hello

Because the Var is omitted here, the message becomes a global variable, so as long as the test () function is called once, the variable is defined and can be accessed anywhere outside the function.

Omit var to define a variable: The variable can be accessed globally whenever a function that defines the variable is called once. This method of defining variables is also an implicit declaration

iv. scope and scope

<script type= "Text/javascript" >//define an output functionfunctionOutPut (s) {Document.writeln (s);}//Global VariablesvarI=0; //Defining external functionsfunctionouter () {//Accessing global VariablesOutPut (i);//0//define a class-part functionfunctioninner () {//Defining local Variablesvari = 1; //I=1: If implicit declaration is used, then the global variable i is overwrittenOutPut (i);//1} inner (); OutPut (i); //0} outer (); </SCRIPT>

Results: 0,1,0

<script type= "Text/javascript" > function  demofunction (s) {Document.writeln (s)}  Var i=0;      function  Test () {demofunction (i);        function  innerfunction () {demofunction (i);      var i=1;    Demofunction (i);     } innerfunction (); Demofunction (i);} Test (); </script>

Results: 0 undefined 1 0

The local variables declared in the JS function body are valid throughout the function, so var i = 1 in the code above, and in the inner function, in fact the explicitly declared variable i is compiled into the calling object at precompiled time, unlike an implicitly declared variable that is defined as a global variable at the time of interpretation. Just when the output (i) is called, the variable is not initialized, and the local variable i is an unassigned variable, not an undefined variable, so the undefined is output. The above code is equivalent to the following code:

functionvar////I=1//

Learn JavaScript three (variable) from scratch

Related Article

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.