# Console.log不准的问题

{% embed url="<https://v.douyin.com/rREQmeq/>" %}

{% embed url="<https://www.bilibili.com/video/BV1M34y1h7RK/>" %}

前两天

一个小伙伴给我提了一个号称

特别诡异的问题

其实这个问题之前我也遇到过

我简单给大家模拟了一下哈

就是点这个按钮呢会走这个方法

然后这个方法前两行就特别简单

先烙个一下这个数组

然后烙个一下数组第0个元素

来看下效果哈

打开数组的第0个元素是 n 底泛的

但是呢我们把数组展开看

里边是有元素的

那为什么会这样呢

其实就是这个展开的问题

呃这个数组打印的时候确实为空

但是当我们第一次展开变量的那一刻

系统

会读取一下这个数组的最新的状态 (类似引用传递了变量）

把它展示给我们

那在我这个例子里边呢

他下边是还有代码的

就是

打印完以后他往数组里放了一个元素

所以呢

在我们展开的那一刻

其实数组里边已经有值了

只是在打印的时候还没有值

那只要我们展开过这个变量

再往里放值

我们再重新展开是没有用的

我们可以验证一下哈

我们先声明一个变量具有 tmp 哈

好然后呢我们打印一下

tmp

打印的时候现在是空对吧

肯定里面没有值但是我先不展开

我再往里面push一个

push好之后呢我再展开大看

它里边已经有123了吧

然后再push一个

好我们再展开大看没有那个但是呢我们再log一下

tmp

他看他已经有123和234了

所以啊

我们在调试的时候

如果打印的是一个对象或者一个数组

大家要小心有

可能这个对象和数组

它又变化过

但是呢你在这里展开变量的时候

他只能看到这个变量在你展开

那一刻的状态


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sunzsh.gitbook.io/xiaoshan.bug/console.log-bu-zhun-de-wen-ti.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
