声明前登录Google Chrome控制台的JavaScript对象属性 [英] Javascript Object Property logged in Google Chrome Console before Declaration

查看:127
本文介绍了声明前登录Google Chrome控制台的JavaScript对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用谷歌浏览器版本28.0.1500.95 m 控制台测试一些JavaScript,并且希望对它的工作原理有所了解:看看代码如下:

  var obj = {
a:99,$ b $:function(){}


console.log(obj.a)
console.log(obj.z)
console.log(obj)

盎司= 100;

演示

结果如下:

  99 
$ undefined
Object {a:99,f:function}
a:99
f:function(){}
z:100
__proto__:Object

我的问题是,为什么 z 在结果中可见直到日志之后才被声明?



我假设这是控制台的工作方式,而不是JavaScript中的一些奇怪的范围规则,我不知道



任何人都可以告诉我这里发生了什么事情吗?

解决方案

控制台中的对象最初显示为 Object ,并在您点击箭头时展开。



展开对象时的i图标,当您将其悬停时,您将看到答案:

下面的对象状态是在第一次展开时捕获的 >



展开后看到的是对象在扩展时的状态,而不是当您调用 log()


I've been testing some JavaScript using the Google Chrome version 28.0.1500.95 m console and am looking for a bit more understanding of how it works: Have a look at the code below:

var obj = {
    a: 99,
    f: function() { }
}

console.log(obj.a)
console.log(obj.z)    
console.log(obj)

o.z = 100;

Demo

This outputs the following results:

99 
undefined 
Object {a: 99, f: function}
a: 99
f: function () { }
z: 100
__proto__: Object

My question is, why is z visible in the results when it wasn't declared until after the log?

I'm assuming this is something with how the console works and not some weird scoping rule in JavaScript, that I'm unaware of?

Can anyone tell me what's happening here please?

解决方案

The object in the console initially is shown as Object and expanded when you click on the arrow.

There is an i-icon when you expand the Object, when you hover it you'll see the answer:

object state below is captured upon first expansion

What you see after the expansion is the state of the object at the time of the expansion, not the state at the moment when you call log()

这篇关于声明前登录Google Chrome控制台的JavaScript对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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