即使对象属性显示在控制台日志中也无法访问 [英] Can't access object property, even though it shows up in a console log

查看:59
本文介绍了即使对象属性显示在控制台日志中也无法访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面,您可以看到这两个日志的输出。第一个清楚地显示了我尝试访问的具有属性的完整对象,但是在下一行代码中,我无法使用 config.col_id_3 (看到截图中的未定义?)。谁能解释一下?除了 field_id_4 外,我还可以访问其他所有属性。

Below, you can see the output from these two logs. The first clearly shows the full object with the property I'm trying to access, but on the very next line of code, I can't access it with config.col_id_3 (see the "undefined" in the screenshot?). Can anyone explain this? I can get access to every other property except field_id_4 as well.

console.log(config);
console.log(config.col_id_3);

这是在控制台中打印的行

This is what these lines print in Console

推荐答案

console.log(anObject)的输出具有误导性;仅当通过单击> 展开控制台中显示的对象树时,才能解决显示的对象的状态。当您 console.log 选择对象时,它不是对象的状态。

The output of console.log(anObject) is misleading; the state of the object displayed is only resolved when you expand the Object tree displayed in the console, by clicking on >. It is not the state of the object when you console.log'd the object.

相反,请尝试 console.log(Object.keys(config))甚至是 console.log(JSON.stringify(config)),您将在调用 console.log 时看到键或对象的状态。

Instead, try console.log(Object.keys(config)), or even console.log(JSON.stringify(config)) and you will see the keys, or the state of the object at the time you called console.log.

您(通常)会在您的 console.log 调用后 之后找到要添加的密钥。

You will (usually) find the keys are being added after your console.log call.

这篇关于即使对象属性显示在控制台日志中也无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆