Javascript Chrome控制台bug?
这个问题没有任何疑问或问题。显然,我似乎在chrome控制台上偶然发现了一个bug 我使用以下代码:Javascript Chrome控制台bug?,javascript,ecmascript-6,google-chrome-devtools,web-inspector,Javascript,Ecmascript 6,Google Chrome Devtools,Web Inspector,这个问题没有任何疑问或问题。显然,我似乎在chrome控制台上偶然发现了一个bug 我使用以下代码: console.log("Before: ", this.selectedScorecard.scorecard_attributes); let attribute = this.selectedScorecard.scorecard_attributes.find(item => item.id === null || item.id === undefined) if(attrib
console.log("Before: ", this.selectedScorecard.scorecard_attributes);
let attribute = this.selectedScorecard.scorecard_attributes.find(item => item.id === null || item.id === undefined)
if(attribute) {
let length = this.selectedScorecard.scorecard_attributes.length;
this.selectedScorecard.scorecard_attributes.splice(length-1, 1);
console.log("After: ", this.selectedScorecard.scorecard_attributes);
}
好的,这个属性是一个数组,它最初是一个长度为2的数组。
现在我从数组中拼接一个项目,并在拼接前后打印它的值
在chrome控制台中,数组前的代码段显示(2),表示长度为2,但在数组本身中,它显示长度为1,并且在“数组前控制台”中仅显示一项
在“后控制台”中,事情显然与预期一样
为了更好地理解,我附上了一张图片
我很好奇,有人知道这件事吗?以前有人遇到过这种情况吗?还是只有我注意到了这一点?当console.log运行时,chrome会同步注销其中的内容摘要。数组有2个元素,因此摘要记录了一(2)个元素。Chrome会保留对阵列的引用,以防您以后决定要查看更多数据,但它不会克隆整个阵列
稍后,当您单击展开数组时,它会显示更多详细信息,但它是根据展开数组时的外观显示的,而不是根据log语句最初出现的时间显示的。因为它只有一个元素,所以它显示的就是这个元素。如果不想更改对象,则需要对其进行深度复制。要解决这个问题:
console.log(“Before:”,JSON.parse(JSON.stringify(this.selectedscorcard.scorcard_attributes))代码>欢迎来到副作用世界。在每一行中悬停[i]
。它告诉您该值是在您展开行时计算的。我还要补充一点,记录对象在给定时刻的状态的简单解决方法是使用console.log(JSON.stringify(obj))