Typical examples are as follows:

  alert(a)   // function
  a()    / / 10
  var a=3;
  function a() {
    alert(10)
  }
  alert(a) / / 3
  a=6;
  a()  // There is no function a
Copy the code

Why did this happen? Look at the example below

Sample a

  /*1 alert(a); I pay therefore I am 5; * /

  /*2 alert(a) a=1; A is not defined */
  
  Var function parameter is found according to var; Fn1 =function fn1() {alert(2)} Function fn1() {alert(2)} function fn1() {alert(2)} function fn1() {alert(2)} Line by line: = + - *, / % ++ --! Parameters... alert(a) var a=1; function fn1() {alert(2)} */
Copy the code

Example 2

  /* js preparse will leave only one,!! A =function a() {alert(2)} =function a() {alert(2)}; A =function a() {alert(4)} */

  // Start reading line by line
  alert(a);//function a() {alert(4)}
  var a=1;// the expression a is changed to 1;
  alert(a)/ / 1
  function a() {alert(2)}// is a function declaration, not an expression, that does not change the value of a
  alert(a);/ / 1
  var a=3;
  alert(a);/ / 3
  function a() {alert(4)};
  alert(a);/ / 3

  a()A =3; a=3;
Copy the code

Examples of three

<! --<script>-->
    <! --//1.
    <! - executive -- > / / 2.
<! --</script>-->

<! --<script>-->
    <! --//3.
    <! - executive -- > / / 4.
<! --</script>-->

 <! -- 1. <script> var a=3; </script> <script> alert(a); //3 </script> -->

  <! -- 2. <script> alert(a); / / an error; a is not defined </script> <script> var a=3; </script> -->

Copy the code

Examples of four

  var a=1;
  function fn1() {
    alert(a);//undefined
    var a=2;
  }
  fn1();
  alert(a);/ / 1
  Fn1 =function fn1() {alert(a); var a=2; } 2. Line by line: a=1; Function call 1) preparse local A = undefined 2) read code line by line alert(a)=> undefined A =2; * /
Copy the code

4-1 sample

  var a=1;
  function fn1() {
    alert(a);/ / 1
     a=2;
  }
  fn1();
  alert(a);/ / 2
  Fn1 =function fn1() {alert(a); a=2; } 2. Line by line: a=1; Function call 1) a layer of the scope of the preliminary analytical along this jump a layer to find (scope chain) 2) interpreting the code line by line alert (a) / / the current scope, down the scope to jump on a layer of a layer to find (scope chain) a = 2 is also a pop-up 1 first, then pop up 2 * /

Copy the code

4-2 examples

  var a=1;
  function fn1(a) {// The argument is equivalent to a local variable.
    alert(a);//undefined
     a=2;
  }
  fn1();
  alert(a);/ / 1
  Function fn1(a) {alert(a); function fn1(a) {alert(a); a=2; } 2. Line by line: a=1; 1) preparse a= undefined 2) read a=2 line by line; * /
Copy the code

Sample 4 to 3

  var a=1;//标志1
  function fn1(a) {// The argument is equivalent to a local variable.
    alert(a);//1, this a is different from the a at flag 1, this A is local, the outside a is global
    a=2;
    alert(a)/ / 2
  }
  fn1(a);// This a is global
  alert(a);/ / 1
  Function fn1(a) {alert(a); function fn1(a) {alert(a); a=2; } 2. Line by line: a=1; Function fn1(var a=1); function fn1(var a=1)
Copy the code

4-4 examples

  var a=1;
  function fn1(a) {       Var a; A =1(outside)
    alert(a);/ / 1
     var a=2;
     console.log(a);/ / 2
  }
  fn1(a);
  alert(a);/ / 1

Copy the code

Sample 4 to 5

  var a=1;
  function fn1(a) { 
    arguments[0] =3;
    alert(a);/ / 3
    var a=2;
    console.log(a);/ / 2
  }
  fn1(a);
  alert(a);/ / 1

Copy the code

Example number 5 wants to get the value inside the function

  // Want to get the value inside the function
  / / a
  /* var str='' function fn1() { var a='1'; str=a; } fn1(); alert(str) */
  / / 2
  function fn2() {
    var a='100';
    fn3(a);
  }
  fn2()
  function fn3(b) {
    alert(b)
  }

Copy the code

Example 6

  If (1){var a=1; if(1){var a=1; } alert(a); The {} of the for loop is also not a block-level scope; the scope is marked by parsing and then executing */


  /* alert(a); //undefined if(true){ var a=1; } * /

  /* alert( fn1 ); // Underfine (the latest browsers), IE10 and below are able to pop functions... if(true){ var a=1; function fn1() { alert(123) } }*/


  /* / alert(fn1); var a=1; function fn1() { alert(123) } if(true){ }*/

Copy the code

Example 7

<input type="button" value="1">
<input type="button" value="2">
<input type="button" value="3">
Copy the code

    /* //1. window.onload=function () { var oBtn=document.getElementsByTagName('input'); for(var i=0; i

    
/* //2 window.onload=function () { var oBtn=document.getElementsByTagName('input'); for(var i=0; i
Copy the code