I heard you want to interview for front-end?
Golden Three Silver Four Season, the front-end has been a hot field in recent years, and the atmosphere is particularly strong. My friend Xiaowei has been interviewing like crazy recently, and has met many interesting interviewers and interesting interview questions. I’m here to help. Let me paraphrase this troublemaking boy.
The following is a story from a friend of mine, it’s really not me.
for (var i = 0; i < 5; i++) { console.log(i); }
"Xiaowei, tell me what these lines of code will output?"
When the interviewer typed these lines of code in Sublime, I was a little confused. Clam? Isn't this the simplest loop? Is there a trap? I thought about it. This seems to be very similar to the closure question I saw. Did the interviewer not finish writing it? It's poisonous.
"It should output 0 to 4 directly...", I said weakly.
"Yes, don't be nervous. There are no traps in this question. I just write it casually."
(Excuse me? Interviewer, are you here to make fun of me? I'm scared to death! )
"Then what are you looking at the output of these lines of code?"
for (var i = 0; i < 5; i++) { setTimeout(function() { console.log(i); }, 1000 * i);}
Um, what the hell, why is it not the closure question I have memorized so many times? Let me think. setTimeout will delay execution, so when console.log is executed, i has actually become 5. Yes, that's it. How can it be so difficult for me to do something so simple?
"It should start to output a 5, and then output a 5 every second, a total of 5 5s."
"Yes, how should it be changed to output 0 to 4? "
Finally I'm familiar with it. Just add a closure and it's solved. It's stable!
for (var i = 0; i < 5; i++) { (function(i) { setTimeout(function() { console.log(i); }, i * 1000); })(i); }
"Very good, can you tell me what will happen if I delete this i?"
for (var i = 0; i < 5; i++) { (function() { setTimeout(function() { console.log(i); }, i * 1000); })(i); }
"In this case, there is no internal reference to i. In fact, it will It becomes output 5. "
"Very good, let me change it for you and see what the output will be?"
for (var i = 0; i < 5; i++) { setTimeout((function(i) { console.log(i); })(i), i * 1000); }
Huh? What the hell, what is going on, let me think about it. Here an immediate execution function is passed to setTimeout. Well, setTimeout can accept functions or strings as parameters, so what is the immediate execution function here? It should be undefined, which is equivalent to:
setTimeout(undefined, ...);
And the immediate execution function will execute immediately, so it should be Output immediately.
"It should output 0 to 4 immediately."
"Oh, not bad. The last question, do you know Promise?"
"It's okay. ..."
"OK, then try this question."
setTimeout(function() { console.log(1) }, 0); new Promise(function executor(resolve) { console.log(2); for( var i=0 ; i<10000 ; i++ ) { i == 9999 && resolve(); } console.log(3); }).then(function() { console.log(4); }); console.log(5);
WTF! ! ! ! I want to be quiet!
This question should examine the running mechanism of my JavaScript. Let me sort out my thoughts.
First encounter a setTimeout, so a timer will be set first, and after the timer ends, the function will be passed to the task queue, so 1 will definitely not be output at first.
Then there is a Promise, the function inside is executed directly, so it should output 2 3 directly.
Then, the then of Promise should be placed at the end of the current tick, but still in the current tick.
Therefore, 5 should be output first, and then 4.
Finally, the next tick is 1.
“2 3 5 4 1”
“Okay, let’s wait for the next round of interviews.”
So easy! Mom no longer has to worry about my interviews.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The Node service built based on non-blocking and event-driven has the advantage of low memory consumption and is very suitable for handling massive network requests. Under the premise of massive requests, issues related to "memory control" need to be considered. 1. V8’s garbage collection mechanism and memory limitations Js is controlled by the garbage collection machine

PHP and Vue: a perfect pairing of front-end development tools. In today's era of rapid development of the Internet, front-end development has become increasingly important. As users have higher and higher requirements for the experience of websites and applications, front-end developers need to use more efficient and flexible tools to create responsive and interactive interfaces. As two important technologies in the field of front-end development, PHP and Vue.js can be regarded as perfect tools when paired together. This article will explore the combination of PHP and Vue, as well as detailed code examples to help readers better understand and apply these two

With the development of Internet technology, front-end development has become increasingly important. Especially the popularity of mobile devices requires front-end development technology that is efficient, stable, safe and easy to maintain. As a rapidly developing programming language, Go language has been used by more and more developers. So, is it feasible to use Go language for front-end development? Next, this article will explain in detail how to use Go language for front-end development. Let’s first take a look at why Go language is used for front-end development. Many people think that Go language is a

In front-end development interviews, common questions cover a wide range of topics, including HTML/CSS basics, JavaScript basics, frameworks and libraries, project experience, algorithms and data structures, performance optimization, cross-domain requests, front-end engineering, design patterns, and new technologies and trends. . Interviewer questions are designed to assess the candidate's technical skills, project experience, and understanding of industry trends. Therefore, candidates should be fully prepared in these areas to demonstrate their abilities and expertise.

Django is a web application framework written in Python that emphasizes rapid development and clean methods. Although Django is a web framework, to answer the question whether Django is a front-end or a back-end, you need to have a deep understanding of the concepts of front-end and back-end. The front end refers to the interface that users directly interact with, and the back end refers to server-side programs. They interact with data through the HTTP protocol. When the front-end and back-end are separated, the front-end and back-end programs can be developed independently to implement business logic and interactive effects respectively, and data exchange.

Golang can be used as a front-end. Golang is a very versatile programming language that can be used to develop different types of applications, including front-end applications. By using Golang to write the front-end, you can get rid of a series of problems caused by languages such as JavaScript. For example, problems such as poor type safety, low performance, and difficult to maintain code.

As a C# developer, our development work usually includes front-end and back-end development. As technology develops and the complexity of projects increases, the collaborative development of front-end and back-end has become more and more important and complex. This article will share some front-end and back-end collaborative development techniques to help C# developers complete development work more efficiently. After determining the interface specifications, collaborative development of the front-end and back-end is inseparable from the interaction of API interfaces. To ensure the smooth progress of front-end and back-end collaborative development, the most important thing is to define good interface specifications. Interface specification involves the name of the interface

As a fast and efficient programming language, Go language is widely popular in the field of back-end development. However, few people associate Go language with front-end development. In fact, using Go language for front-end development can not only improve efficiency, but also bring new horizons to developers. This article will explore the possibility of using the Go language for front-end development and provide specific code examples to help readers better understand this area. In traditional front-end development, JavaScript, HTML, and CSS are often used to build user interfaces
