JS pre-analytical case studies

Case number one:

1 var a = 25;
2 function abc () {
3     alert(a);//undefined
4     var a = 10;
5 }
6 abc();
7 console.log(a);//25

The result is: undefined  and 10 , the actual code logic is as follows:

. 1  var A; // declare variables
 2 A = 25 ; // assignment
 . 3  function ABC () {
 . 4      var A; // declare variables
 . 5      Alert (A); // A only declared, but no assignment, it is undefined
 . 6      A = 10 ; // assignment
 . 7  }
 . 8 ABC (); 
. 9 the console.log (A); // 25

 

Case II:

console.log(a);
function a() {
  console.log('aaaaa');
}
var a = 1;
console.log(a);

result:

ƒ a() {
      console.log('aaaaa');
    }
1

 

Actual code logic is as follows:

var A;
 function A () { 
  the console.log ( 'AAAAA' ); 
} 
the console.log (A); // A where the function name is a, the output is a function of the code 
A =. 1 ; 
the console.log (A ); // 1

 

Case 3:

var a = 18;
f1();
function f1() {
  var b = 9;
  console.log(a);
  console.log(b);
  var a = '123';
}

Results: undefined and 9 , the actual code logic is as follows:

var A; 
A = 18 is ;
 function F1 () {
   var B;
   var A; 
  B =. 9 ; 
  the console.log (A); // A statement only, no assignment, it is undefined 
  the console.log (B); // . 9 
  A = '123' ; 
} 
F1 ();

 

Case Four:

f1();
console.log(c); //9
console.log(b); //9
console.log(a); //报错
function f1() {
  var a = b = c = 9;
  console.log(a); //9
  console.log(b); //9
  console.log(c); //9
}

The results are:

 

 Actual code logic is as follows:

function F1 () {
   var A; 
  A =. 9; // local variables, declared by var 
  B =. 9; // implicit global variables, declared useless var 
  C =. 9; // implicit global variables 
  console.log ( A); // . 9 
  the console.log (B); // . 9 
  the console.log (C); // . 9 
}     
F1 (); 
the console.log (C); // . 9 
the console.log (B); // . 9 
the console.log (A); // given

 

Case 5:

f1 ();
was f1 = function () { 
  console.log (a); 
  was a = 10 ; 
}

The results will be given, the actual code logic is as follows:

var F1; // declare variables 
F1 (); // F1 is a variable, the function is not, it will be given IS Not A function F1 
F1 = function () { 
  the console.log (A); // this is not performed 
  var = 10 A ; 
}

 

Guess you like

Origin www.cnblogs.com/muzidaitou/p/12455671.html
Recommended