1. Introduction

For a Web front end, when the interview, it is inevitable to encounter javascript interview questions. For myself. There are several interview questions, some I met in the interview, some I read on the Internet, but they are all very impressive. Today, I will briefly analyze some of the interview questions THAT IMPRESSED me! The main purpose is to let the partners learn something, such as in the future encounter similar situations, remember not to fall pit!

2. The parsing

Preparse: before running js in the current scope, declare var and function keywords in advance, but do not assign values (personal opinion)

Impressed by the pre-analysis, not because it is difficult, but to be careful, a little careless, the answer is wrong! I met more than one problem of pre-analysis, there are two I can remember now, I say!

2-1. Preparse 1

alert(a)
a();
var a=3;
function a(){ alert(10) } alert(a) a=6; a(); ------------ secant line ------------------ alert(a) a(); var a=3; var a=function(){
    alert(10)
}   
alert(a)
a=6;
a(); 
Copy the code

When I saw this code, I got it wrong. Later consulted a friend, and then their own understanding, straighten out! In fact, there are two, the first variable declaration ahead of time, the second function declaration priority variable declaration! Function a(){alert(10)}; function a(){alert(10)}; Var alert(10) =3; var alert(10) =3; Alert (a) = alert(a) = alert(a) = alert(a) = alert(a) As mentioned earlier, preparse is to declare the var and function keywords in advance, but not to assign them. So at first it’s underfind, and then it’s an error because by the time a() is executed, a is not a function.

Var a= ()function(){alert(10)} // Function declaration is better than variable declarationfunction a(){
    alert(10)
} 
Copy the code

2-2. Preparse and scope

var a=0;
function aa() {alert (a) a = 3} / / the result is nothing happened, because must perform aa function will alert (0) -- -- -- -- -- -- -- -- -- -- -- -- line 1 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the var a = 0;function aa(){ alert(a) var a=3 } aa(); / / underfind in aa function, var a = 3, then in aa scope inside, is this a variable declaration in advance, but not the assignment, so it is underfind -- -- -- -- -- -- -- -- -- -- -- -- line 2 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the var a = 0;functionAa (a) {alert var (a) a = 3} aa (5) the alert (a) / / 5, 0 in the function, the parameters of a priority than variable a -- -- -- -- -- -- -- -- -- -- -- -- line 3 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the var a = 0;functionAa (a){alert(a) a=3} aa(5) alert(a) =3 It is the argument a ------------ dividing line 4------------------ var a=0;functionAa (a){alert(a) var a=3 alert(a)} aa(5) {alert(a) var a=3 alert(a)} aa(5) //var a=0; //var a=0; //functionaa(a){ // var a=5; // alert(a) // a=3 // alert(a) //} //aa(5) //2. Var a=0; //var a=0; //functionaa(a){ // var a; A =5; // a=5; / / / / alert (a) a = 3 / / alert (a) / /} / / aa (5) -- -- -- -- -- -- -- -- -- -- -- -- line 5 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the var a = 0;functionAa (a){alert(a) a=3 alert(a)} aa() alert(a) =3 alert(a)} aa() alert(a) 3 It's not the global variable a, and then the alert(a) down here is the parameter a // and then the alert(a) at the end, you get the ideaCopy the code

3. Loop and recursion

3-1. Fibonacci arrays

I won’t go into that. It’s simple, but it’s classic. The current term is equal to the sum of the first two terms

var arr=[];
for(var i=0; i<10; i++ ){ i<=1? arr.push(1):arr.push(arr[i-1]+arr[i-2]); } console.log(arr)Copy the code

3-2. Data array

123454321, 23456765432 how do I do that? The way I did it was I did it in two steps, I showed you the first one, and then I showed you the second one

//01234543210 // Show the previous 01234 //n: the beginning number m: the end numberfunction num1(n,m){
    for(var i=n; i<m; I ++){// show the 543210 console.log(I);if(i===m-1){
            num2(n,m)
        }
    }
}
function num2(n,m){
    for(var i=m;i>=n;i--){
        console.log(i)
    }
}
num1(2,5)  //2345432Copy the code

That’s a lot of code, and then we looked at this

function num(n,m){
    console.log(n);
    if(n<m){ num(n+1,m); console.log(n); Num}} (2, 5) / / 2345432Copy the code

Explain the following

1. Run num(2,5), console.log(2); - > num (3, 5); -> console.log(2); / / execution num (3, 5); This is equivalent to console.log(3); - > num (4, 5); -> console.log(3); Console.log (2); -> console.log(3); - > num (4, 5); -> console.log(3); -> console.log(2); Then console.log(2); -> console.log(3); -> console.log(4); - > num (5, 5); -> console.log(4); -> console.log(3); -> console.log(2); And finally console.log(2); -> console.log(3); -> console.log(4); -> console.log(5); -> console.log(4); -> console.log(3); -> console.log(2);Copy the code

4. Other

4-1

function foo1()
{
 return {
     bar: "hello"
 };
}
 
function foo2()
{
 return
 {
     bar: "hello"
 };
}
var a=foo1();
var b=foo2();
console.log(a) //Object {bar: "hello"} console.log(b) //underfind //Copy the code

4-2

The topic that sees on the net, I transform the JS interview question that 80% applicant all fail below myself

for(var i = 0; i < 5; i++) { console.log(i); } console.log(i); // You should know this soon, 012345for (var i = 0; i < 5; i++) {
 setTimeout(function() { console.log(i); }, 1000); } console.log(i); // The answer is 5 55555 // InsetBefore Timeout is executed,forThe loop has already been executed, the value of I is already 5, so the first one is executed, and the last one is console.log(I); / / inforCustom 5 at once while loopingsetTimeout, about a second later, is output 55555for (var i = 0; i < 5; i++) {
 (function(j) { // j = i
  setTimeout(function() { console.log(j); }, 1000); })(i); } console.log(i); Var output = 5 01234 var output = 5 01234 var output = 1function (i) {
 setTimeout(function() {
  console.log(i);
 }, 1000);
};
 
for(var i = 0; i < 5; i++) { output(i); } console.log(I); // So the answer is 5 01234. // So the answer is 5 01234for (let i = 0; i < 5; i++) {
 setTimeout(function() { console.log(i); }, 1000); } console.log(i); // Error 01234 is reportedletIt's not varCopy the code

5. Summary

First of all, I want to say that these are some of the most impressive topics in my own experience, not necessarily common topics. Then, this article is kind of a note for me, a record of the topics I’ve come across. I am not sending these interview questions to my friends in order to make them remember the questions and answers, or to prepare for the interview. It is meaningless and unrealistic! My purpose is to let you can learn, through the topic to know some of the principles and the mechanism of operation, or to know some of the possible ‘traps’. In addition, I have encountered a lot of actual operations, such as array deduplication, shuffling the array, counting the number of array elements, the number of occurrences of each character string, get the address link parameters and so on. These questions not only appear in the interview questions are often used in the actual project development, partners can learn by themselves. Of course, I have also encapsulated some functions, that is, to implement the above mentioned operations of the function, I will soon write an article about which functions I encapsulated, which commonly used functions, and then share. Any need to correct, or good suggestions, also welcome to point out!


— — — — — — — — — — — — — — — — — — — — — — — — — gorgeous line — — — — — — — — — — — — — — — — — — — — want to know more, pay attention to focus on my WeChat public number: waiting for book cabinet