从易到难,循序渐进的过程啦,
现在都是零零碎碎,之后再整理啦
1. 点击input依次输出的值
const text = document.getElementById('text');
text.onclick = function (e) {
console.log('onclick')
}
text.onfocus = function (e) {
console.log('onfocus')
}
text.onmousedown = function (e) {
console.log('onmousedown')
}
text.onmouseenter = function (e) {
console.log('onmouseenter')
}
// 答案:
'onmouseenter'
'onmousedown'
'onfocus'
'onclick'
解析:
onmouseenter
: 当鼠标指针移动到图像时执行
onmousedown
: 当用户按下鼠标按钮执行
onfocus
: 当 input 输入框获取焦点时执行
onclick
: 当按钮被点击时执行
Promise程序题:
event loop的执行顺序:
一、整个脚本作为一个宏任务执行
二、执行过程中同步代码直接执行,宏任务进入宏任务队列,微任务进入微任务队列
三、当前宏任务执行完出队,检查微任务列表,有则依次执行,直到全部执行完
宏任务: script、setTimeout、setInterval、setImmediate、I/O、UI readering
微任务: MutationObserver、 Promise.then()或catch()、Node独有的process.nextTick
(1):
setTimeout(() => {
console.log('timer1');
setTimeout(() => {
console.log('timer3')
}, 0)
}, 0)
setTimeout(() => {
console.log('timer2')
}, 0)
console.log('start')
依次输出:
‘start’
‘timer1’
‘timer2’
‘timer3’
解析:
- 整个脚本执行视为一个宏任务为
宏1
, 所以最先输出’start‘,- setTimeout是宏任务,外层的第一个setTimeout进入第二轮宏任务为
宏2
,第2个setTimeout进入第二轮宏任务为宏3
,’timer3‘的setTimeout为宏4
- 按照宏任务的执行顺序
(2):
setTimeout(() => {
console.log('timer1');
Promise.resolve().then(() => {
console.log('promise')
})
}, 0)
setTimeout(() => {
console.log('timer2')
}, 0)
console.log('start')
依次输出:
‘start’
‘timer1’
‘promise’
‘timer2’
解析:
- 整个脚本执行视为一个宏任务为
宏1
, 所以最先输出’start‘,- setTimeout是宏任务,外层的第一个setTimeout进入第二轮宏任务为
宏2
,第2个setTimeout进入第二轮宏任务为宏3
,- Promise.then()是微任务,为第一轮微任务,为
微1
- 执行第一轮宏任务输出:
'start'
,
进入宏2,输出'timer1'
,执行完第二轮宏任务,发现内部发现微1
进入微1,输出'promise'
再进入宏3,输出'timer2'
Class程序题:
1.
class Person {
constructor(name) {
this.name = name
}
}
const person= new Person("平井缘")
console.log(typeof person) // Object
解析: 通过new来调用构造函数,将会生成构造函数Person的实例对象