无法访问对象属性,即使它存在。返回undefined [英] Can't access object property, even though it exists. Returns undefined

查看:169
本文介绍了无法访问对象属性,即使它存在。返回undefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

令我感到困惑。我不知道怎么解释这个,但这是我的调试代码的样子。在它下面,您可以看到这两个日志的输出。第一个清楚地显示了我正在尝试访问的属性的完整JavaScript对象,但下一行代码我无法使用config.col_id_3访问它(请参阅屏幕截图中的undefined?)。有谁能解释一下?我也可以访问除field_id_4之外的所有其他属性。

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

这是这些console.log()在控制台中打印的内容



解决方案

console.log(anObject)的输出具有误导性;只有在控制台中展开> 时,才会解析显示的对象的状态。当 console.log 'd对象时,对象的状态。



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



您(通常)会在 b $ b

This is baffling me. I don't know how else to explain this, but here is what my debug code looks like. Below it you can see the output from these two logs. The first clearly shows the full JavaScript object with the property I'm trying to access, but the very next line 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 console.log()s print in Console

解决方案

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

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.

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

这篇关于无法访问对象属性,即使它存在。返回undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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