JavaScript variable Scope detailed

Source: Internet
Author: User
Tags variable scope
          This article is mainly on the JavaScript variable scope for detailed analysis of the introduction, the need for friends can come to the reference, I hope that we have to help the scope of variables refers to the visibility of variables, And the life cycle is the variable from another angle.         JS The scope of the variables are divided into global variables and local variables, defined within the function called local variables, functions called global variables outside. ("Called global variable" outside the function is relative, and the premise discussed here is the variable explicitly declared with VAR, the variable defined in the function without VAR is the default global variable, of course, ignoring VAR declaration variable is not supported).     Code as follows: var glob = 4;//function declaration global variable   function fun () {      var height = 20;//function with VAR declared local variable & nbsp     weight = 50; The global variable  }  fun ();  alert (weight);    JS does not have a block-level scope in the function, which is contained in braces {}. In Java, there is. Write the following code code in the Main method: public static void Main (String ... args) {  for (int i=0;i<5;i++) { }    &NB Sp {          int j=10;     }      int z = 20;      Sy Stem.out.println (i); I is not visible, the grammar analysis times is wrong, that is, compile not through       SYSTEM.OUT.PRINTLN (j); J is not visible, the grammar analysis times is wrong, that is, compilation does not pass       SYSTEM.OUT.PRINTLN (z); Z Visible, output 20 }    But if in JS     codeas follows: for (Var i=0;i<5;i++) { }  var obj = {name: ' Lily '};  for (var attr in obj) { }  {  &N Bsp var j=10; }  Alert (i)//Output 4, no block-level scope   alert (attr); Output name, no block-level scope   alert (j);//Output 10, no block scope     This also illustrates the problem of avoiding the use of a For loop at the same time declaring variables globally, otherwise polluting the global naming range.   Of course, the LET keyword declaration variable (see HTTPS://DEVELOPER.MOZILLA.ORG/CN/NEW_IN_JAVASCRIPT_1.7) is presented in JS1.7, and it only acts on the scope of the For statement.       Code as follows: for (let i=0;i<5;i++) {     //todo }  alert (i)//run times wrong, hint I not defined & nbsp   JS1.7 need to quote <script type= "application/javascript;version=1.7"/></script>    Ps:firefox2+ implements the JS1.7  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.