loosenrogers / knowledge-points-in-fe-interview Goto Github PK
View Code? Open in Web Editor NEWQuestions collection for FE interview
Questions collection for FE interview
BFC(Block formatting context)直译为"块级格式化上下文"。它是一个独立的渲染区域,只有Block-level box参与, 它规定了内部的Block-level Box如何布局,并且与这个区域外部毫不相干。
BFC规则:计算BFC的高度时,浮动元素也参与计算
所以当元素内部有浮动元素,无法撑起父元素时,可以触发父元素生成BFC,这样父元素内部的浮动子元素就可以撑起父元素
BFC规则:Box垂直方向的距离由margin决定。属于同一个BFC的两个相邻Box的margin会发生重叠
那么不同BFC元素的margin便不会发生重叠;所以当当前元素和垂直方向相邻元素的margin重叠时,可以出发当前元素BFC,这样问题就解决了
使用await调用一个async函数时,会添加一个Microtask。await的内容执行完后,再次添加一个Mictrotask执行后续的代码。
async/await是一个Promise语法糖。对比以下代码:
async function async1() {
console.log('1')
await async2()
console.log('2')
}
async function async2() {
console.log('3')
}
async1();
// 上面的代码约等于:
function async1() {
console.log('1');
new Promise((resolve, reject) => {
console.log('3');
resolve();
}).then(() => {
Promise.resolve().then(() => {
console.log('2');
})
})
}
async1();
第二段代码中,async声明的函数被包了一层Promise。await之后的代码也被包了一层Promise,放在前一个Promise的then()回调中。
如果await后面的代码不是async函数会怎样?对比下边一段代码:
async function async1() {
console.log('1')
await console.log(3);
console.log('2')
}
async1();
// 约等于:
function async1() {
console.log('1');
console.log('3');
Promise.resolve().then(() => {
console.log('2');
})
}
async1();
node中每一次事件循环都要完成以下几个处理阶段,每个阶段都有一个FIFO队列。microtask队列在这些阶段之间处理。
与浏览器不同,node在处理每个阶段的task(宏任务)队列时,会一次性清空队列中的所有任务。然后才转入nextTick队列和MicroTask队列的处理。
Event loop begin:
第1阶段(timer):
- 执行timer队列中的所有task
- 执行nextTick队列中所有的任务(执行过程中注册的nextTick任务会添加到当前队列并执行)
- 执行microtask队列中所有的任务(执行过程中注册的微任务任务会添加到当前队列并执行)
第2-6阶段同上;
...next loop
setTimeout(fn,0) vs. setImmediate(fn)
setTimeout在循环的timer阶段(第一阶段)执行,setImmediate在check阶段(第五阶段)执行。因此setTimeout(fn,0)总是比setImmediate(fn)先执行。
nextTick vs. MicroTask
nextTick会在当前task调用栈执行完后立刻执行。意即nextTick队列的执行总是在MicroTask队列的前边。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.