1、 promise里面如果直接return了?
链接上来: https://juejin.im/post/5e58c618e51d4526ed66b5cf
Promise.resolve(1)
.then(2)
.then(Promise.resolve(3))
.then(console.log)
.then 或者 .catch 的参数期望是函数,传入非函数则会发生值穿透。
第一个then和第二个then中传入的都不是函数,一个是数字类型,一个是对象类型,因此发生了穿透,将resolve(1) 的值直接传到最后一个then里。
输出结果是1
2、深拷贝
这个常常让你手写
来一版本简单的
function deepCopy(original) {
if (Array.isArray(original)) {
return original.map(elem => deepCopy(elem));
} else if (typeof original === 'object' && original !== null) {
return Object.fromEntries(
Object.entries(original)
.map(([k, v]) => [k, deepCopy(v)]));
} else {
// 原始类型值无需拷贝
return original;
}
}
来一版本解决互相引用问题的
const deepClone = (o