Transfer:Juejin. Cn/post / 684490…
1. Introduction
For a Web front-end, it is inevitable to encounter javascript interview questions during the interview. In my own case. There are several interview questions, some I met in the interview, some I read on the Internet, but they all impressed me deeply. Today will be a simple analysis of some of the interview questions I met, impressive! The main purpose of the hope to let small partners learn something, such as later encountered similar situations, remember not to fall pit!
2. The parsing
Preparse: in the current scope, javascript with the var and function keywords will be declared before running, but will not be assigned.
I am impressed by the pre-analysis, not because it is difficult, but to be careful, a little careless, the answer will be wrong! I’ve had not one pre-resolved problem, but two that I can still remember. Let me tell you!
2-1. Preparse 1
alert(a)
a();
var a=3;
function a(){ alert(10) } alert(a) a=6; a(); ------------ ------------------ 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. Consulted a friend later, understand oneself again next, straighten out! The first variable declaration takes precedence over the second function declaration. Function a(){alert(10)} function a(){alert(10)} function a(){alert(10)} function a(){alert(10)} function a(){alert(10)} Alert (10); var a=3; Alert (a); alert(a); alert(a); As mentioned earlier, pre-parsing is a declaration with the var and function keywords, but does not assign values. So it starts as underfind, and then the error is because a() is not a function when it is executed.
// Function expression, same as variable declaration var a=function(){alert(10)} // Function declaration is superior to variable declarationfunction a(){
alert(10)
}
Copy the code
2-2. Preparsing and scoping
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) // 0 Instead, the argument a ------------ split-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 / / / / 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)} aa() alert(a) //underfind 3 0 Alert (a) is also the last alert(a) of the parameter a //, you knowCopy the code
3. Loops and recursion
3-1. The Fibonac array
I don’t want to go into this. It’s simple, but it’s classic. The current term is equal to the sum of the previous 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 arrangement
For example, 123454321 23456765432 how do I do this? What I did was I wrote it in two steps, showing the front and then showing the back
//01234543210 // Display the preceding 01234 //n: start number m: end numberfunction num1(n,m){
for(var i=n; i<m; I ++){// show 543210 console.log(I);if(i===m-1){
num2(n,m)
}
}
}
function num2(n,m){
for(var i=m; i>=n; 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
Num (2,5), console.log(2); - > num (3, 5); -> console.log(2); / / execution num (3, 5); 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); 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
See the topic on the Internet, I transform under 80% applicants fail JS interview questions
for(var i = 0; i < 5; i++) { console.log(i); } console.log(i); // This should be known soon, 012345for (var i = 0; i < 5; i++) {
setTimeout(function() { console.log(i); }, 1000); } console.log(i); // Be careful with this one. The answer is 5 55555 // YessetBefore Timeout executes,forI is already 5, so it starts with execution and ends with console.log(I); / / infor5 at a time as you loopsetTimeout, after about a second, is 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 01234function (i) {
setTimeout(function() {
console.log(i);
}, 1000);
};
for(var i = 0; i < 5; i++) { output(i); } console.log(I);} console.log(I); // I is passed to output as an argument, and I is recorded for each loop. // So the answer is 5 01234for (let i = 0; i < 5; i++) {
setTimeout(function() { console.log(i); }, 1000); } console.log(i); // The result is error 01234letIt's defined, not varCopy the code
5. Summary
First of all, I would like to say that these are some of the topics I have encountered, which are quite impressive, not necessarily common topics. Then, this article can be said to be a note for me, recording the topics I have encountered. The purpose of this essay is not to let the students remember the questions and answers, or to cope with the interview. It is meaningless and unrealistic! My purpose is to let you can learn, through the problem to know some principles and operation mechanism, or to know some possible ‘trap’. In addition, I have encountered a lot of actual operation problems, such as array de-duplication, array shuffling, counting the number of elements in the array, the number of characters in the string, obtaining various parameters of the address link and so on. These questions not only appear in the interview questions more often, in the actual project development will also be often used, small partners can learn by themselves. Of course, I have wrapped some functions myself, that is, the functions that implement the above mentioned operations. I will write a post about this in the near future, which functions I have wrapped, and which common functions I have wrapped, and I will share them later. If you have any corrections or good suggestions, please kindly point them out.