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    varnum1 =; 5

A variable num1is 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. Defining variables outside of 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); //  - }    alert (s+2); // will error }

If the variable inside the function is a duplicate

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!

The concept of global variables is also mentioned above.

A variable that is not defined within any function is a global variable

JS has a Global object window by default, and the variables under global scope are a property bound to the window.

var  num = +; alert (num); // alert (window.num)//

(End

Next chapter Two scope chains of JavaScript pain points

Cond

JavaScript pain point One variable scope

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.