javascript 对象字面量,值/无值?
我正在使用
console.log(p);
console.log(p.datestrshow);
但是控制台中的输出是
当它显然不是时,为什么它是未定义的?
doing
for(i in p)
console.log(i+': ', (typeof p[i] == 'function' ? 'function' : p[i]));
结果为
I am using
console.log(p);
console.log(p.datestrshow);
However the output in the console is
Why is it undefined when it is clearly not?
doing
for(i in p)
console.log(i+': ', (typeof p[i] == 'function' ? 'function' : p[i]));
results in
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您调用
console.log
时,它不会克隆您的p
对象。发生的情况是,当您使用
console.log
时,p.datestrshow
确实未定义,但是当您在控制台中展开p
对象时,它已被定义,并且控制台显示定义了datestrshow
的p
对象的当前状态。您可以在控制台中执行以下测试:
在控制台中运行此代码,然后展开记录的对象。尽管我们在
console.log
之后明确定义了b
,但当您展开对象时它仍然会显示。The
console.log
doesn't make a clone of yourp
object when you call it.What's happening is that
p.datestrshow
is indeed undefined when youconsole.log
, but by the time you expand thep
object in the console, it has been defined, and the console is showing the current state of thep
object withdatestrshow
defined.Here's a test you can do in the console:
Run this code in the console, then expand the object that was logged. Even though we clearly defined
b
after theconsole.log
, it still shows up when you expand the object.