【发布时间】:2021-12-29 14:34:00
【问题描述】:
window.arr = [ 1, 2, 3, 4, 5 ];
window.arr.map(el=>{
console.log(el);
});
asyncFunction(){
window.arr = [ 2, 4, 6 ];
}
如果 asyncFunction 碰巧在 map 仍在运行时更新数组,map 会做什么?
【问题讨论】:
-
Javascript 调用栈是单线程的,所以不会有这种情况
asyncFunction happens to update the array while map is still running -
什么都没有。它已经运行了。
-
……无论如何,您不是在更新数组,而是在更新
arr变量。map在一个对象上被调用,它将继续使用该对象,而不管最初查找该对象的变量发生了什么变化。 -
除了异步之外,您可以在
map内显式更改window.arr,这不会影响调用结果。
标签: javascript arrays ecmascript-6