-2

I was sure this expression

const arr = [1,2,3]
console.log(arr)
arr.push(4)

Should return [1,2,3]. Actually, if write it in the browser console it'll return what expected. But I accidentally open codesandbox and write the same code there and I got as the answer is [1,2,3,4]. And I'm really confused in my knowledge :) I don't know why it's happening. I think it's because of bundler, I tried Parcel and WebPack bundlers both have the same result. But as I said I'm not sure. If anyone knows I would love to read it.

4

1 回答 1

1

它发生在代码驻留在立即执行的脚本中(在页面加载之前),即使在控制台打开时,无论何时刷新页面。在控制台尚未激活时调用 console.log 只会导致对正在排队的对象的引用,而不是控制台将包含的输出。因此,在控制台准备好之前,不会评估数组(或任何对象)。这确实是一个惰性评估的案例。

于 2019-08-28T12:06:16.053 回答