Simple analysis of interview questions

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/ZhaoKaiSX/article/details/102724517

Today and share some bloggers under typical interview questions in the study learned, without further ado, directly on the hard courses.

1. The following figure what will output in the console it?Here Insert Picture Description

The answer && analysis

1> the first logical operators && ||
2> then is to remember this 4

1. As soon as the front || is false, whether it is true or behind || false, the results are returned back values ​​||

2. As long as || foregoing is true, whether it is true or behind || false, the results are returned in front of the values ​​||

3. The long front is && false, whether it is true or behind && false, the results are returned && preceding value

4. The long front is && true, whether it is true or behind && false, the results are returned back to the value &&
Here Insert Picture Description

2. The following codes there are several variables have not been recovered?
      var i = 1;
      var i = 2;
      var add = function(){
          var i = 0;
          return function(){
              i++;
              console.log(i);
          }
      }();
      add();
      
      //A.0个  B.1个  C.2个  D.3个
The answer is three selected D

First we have to know the code recycling rules:

1. Global variables can not be recovered

2. Local variables are recovered, after the function is run once finished, something inside the function will be destroyed

3. As long as referenced by another scope will not be recovered

The reason why there are three variables have not been recovered, the first is the global variable i, the second line will be overwritten by the assignment of the first row, so there is only one, the second
is var add, this variable defines an anonymous function and assign gave add, is not recovered, the third closure is variable i, the local variable closing the package will not be recovered.

Guess you like

Origin blog.csdn.net/ZhaoKaiSX/article/details/102724517