JavaScript pain point One variable scope

Source: Internet
Author: User
Tags variable scope

1. Variables declared with Var are scoped, for example, we declare a variable with VAR in the function

1 ' use strict '; function num () {3   // declare a variable with var num14    var num1 =; 5

A variable NUM1 is declared in the function, and declaring a variable with var in the JS function actually declares a local variable. A local variable is an external access that can only be accessed inside a function .

1  ' use strict '; 2  function  num () {3    // declare a variable with var num14     var num1 =; 5  }6  alert (NUM1);   NUM1 is not defined

2. If a variable is not defined in a function

var num1 = 15; // var declares variable alert (NUM1); //  the function Add () {    alert (num1+5);} add (); //  -

A variable that is not defined in a function is a global variable, and a global variable can be accessed anywhere

3. Non-use of Var

JS allows variables to be used without declaration , without VAR automatically is the global variable

NUM1 = a; alert (NUM1); //  the

* But you'd better not do this, if the introduction of more than one JS file inside there is a non-VAR declaration of global variables will pollute the global, the bug is not easy to troubleshoot. Using strict mode ' use strict ' can avoid this situation

= 15; // REFERENCEERROR:NUM1 is not defined will error

4. Variables within two functions do not affect each other

function Num1 () {    var age =;    alert (age);} NUM1 ()//function  num2 () {    var age = +;    alert (age);} Num2 (); // ;

Nested functions: Intrinsic functions can access variables of external functions, while external functions cannot access intrinsic functions

 function   num () { var  age =15;  function   Num1 () { var  s = Age+10; alert (s);  // 25   + 2); //  } 

If the variable inside the function is the same name
function num () {    var age =;       function Num1 () {     var age =20;     alert (age); //  - } alert (age); //  the   num1 ();} Num ();

The description function is looked up from the inside out, and the external function is automatically masked when the internal function finds the same name as the external function.

5. Variable Promotion

Variables can be declared after they are first used

function num () {   = 5;   Alert (y);   var y;} Num (); //  *

For the above function, the NUM () JavaScript engine sees

functionvar=5; alert (y);}

Only for variable elevation, variable initialization cannot be promoted

function num () {   var y=5;   Alert (y+x);    var x  = +;} Num (); // NaN

Make a good habit of declaring variables at the top of the function!

(End

Global variables to be more

JavaScript pain point One variable scope

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.