some、every和forEach
some、every和forEach三者都可以遍历数组,但是forEach不能中断循环,而every在碰到return false的时候,中止循环。some在碰到return true的时候,中止循环
密封Object.seal和冻结Object.freeze
- 密封,密封对象是不可扩展的对象,而且已有成员的
[[Configurable]]属性被设置为false,这意味着不能添加、删除方法和属性。但是属性值是可以修改的。可以通过Object.isSealed(obj)来判断 - 冻结,冻结对象是最严格的防篡改级别,除了包含密封对象的限制外,还不能修改属性值。可以通过
Object.isFrozen(obj)来判断
Promise执行机制
Promise执行机制详解
Promise.then(func),.then需要先在微任务队列同步注册func,而func的执行是异步的
new Promise((resolve, reject) => {
console.log("外部promise");
resolve();
})
.then(() => {
console.log("外部第一个then");
new Promise((resolve, reject) => {
console.log("内部promise");
resolve();
})
.then(() => {
console.log("内部第一个then");
})
.then(() => {
console.log("内部第二个then");
});
// 这里其实有个隐式的return Promise.resolve()
})
.then(() => {
console.log("外部第二个then");
});
// 执行顺序
// 外部promise > 外部第一个then > 内部promise > 内部第一个then > 外部第二个then > 内部第二个then
/*
内部第一个then先于外部第二个then执行的原因在于,注册微任务是同步的,内部第一个then注册之后,
外部第一个then包裹的同步代码才全部走完,开始走微任务队列
*/