如何打印 javascript 对象?
我有一个对象,
var object= {}
我在对象中放入一些数据,然后我想像这样打印它,
document.write(object.term);
该术语是一个根据不同情况而变化的变量。当我尝试打印它时,它出现未定义。
该怎么做呢?
更新:
这是我正在处理的代码。我想它可能与我上面所说的不一样,因为我是在 selenium 中使用 browsermob 进行的,我只是认为它与 document.write() 类似。这是代码
var numCardsStr = selenium.getText("//div[@id='set-middle']/div[2]/h2");
var numCards = numCardsStr.substr(4,2);
browserMob.log(numCards);
var flash = {}
for(i=0; i<(numCards); i++){
var terms = selenium.getText("//div[@id='words-normal']/table/tbody/tr[" + (i + 2) + "]/td[1]");
var defs = selenium.getText("//div[@id='words-normal']/table/tbody/tr[" + (i + 2) + "]/td[2]");
flash[terms] = defs;
browserMob.log(flash.terms);
}
I have an object
var object= {}
I put some data in the object and then I want to print it like this
document.write(object.term);
the term is a variable that changes depending on different situations. When I try printing this it comes up with undefined.
How would it be done?
Update:
this is the code I am dealing with. I guess it probably isn't the same as what I said above because I am doing it in selenium with browsermob, I just thought it would be similar to document.write(). Here is the code
var numCardsStr = selenium.getText("//div[@id='set-middle']/div[2]/h2");
var numCards = numCardsStr.substr(4,2);
browserMob.log(numCards);
var flash = {}
for(i=0; i<(numCards); i++){
var terms = selenium.getText("//div[@id='words-normal']/table/tbody/tr[" + (i + 2) + "]/td[1]");
var defs = selenium.getText("//div[@id='words-normal']/table/tbody/tr[" + (i + 2) + "]/td[2]");
flash[terms] = defs;
browserMob.log(flash.terms);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您使用 document.write(),那么您很有可能在实例化对象之前尝试引用该对象。我的建议:不要使用 document.write() ,除非您在模板中需要它。对于所有其他目的,请等到页面加载,然后将脚本作为事件处理程序运行。
失败可能还有其他原因,但您的代码示例不够完整,无法进行诊断。
If you're using document.write(), there's a good chance you are trying to reference the object before it's been instantiated. My advice: don't use document.write() unless you need it in a template. For all other purposes, wait till the page loads and then run your script as an event handler.
There could be other reasons for the failure, but your code sample isn't complete enough for a diagnosis.
要将整个对象输出为文本,请使用 JSON 库:
。
这会将对象输出为字符串并缩进 4 个空格以方便阅读。
你要做的是这样的:
这会创建这个对象:
如果你想浏览属性(即“abce”),请执行以下操作:
这将输出:
To output the whole object as text, use a JSON library:
.
This will output the object as a string and indent 4 spaces to make it easy to read.
What you do is this:
This creates this object:
If you want to go through the properties (i.e. "abce"), do this:
This will output:
因为我还没有看到提到这一点:
如果您正在定义一个对象类,例如
MyObj
:然后每当您编写类似的内容时,
它都会显示为
。Because I haven't seen this mentioned yet:
In the case that you're defining an object class, like
MyObj
:And then anytime you write something like
It'll appear as
<MyObject: 2>
.避免 document.write
如果您使用 Firefox,请安装 firebug 并使用它的 控制台 api
相同的控制台 api 也应该在 chrome 中工作。
对于 IE,获取 companion js
在 javascript 中,如果属性名称是事先已知的。如果不是,则:
如果 pn 包含属性名称,则 obj[pn] 应该为您提供值。
Avoid document.write
If you use Firefox, install firebug and use it's console api
The same console apis should work in chrome too.
For IE, get companion js
In javascript, obj.propertyname is used if the property name is known before hand. If it's not, then:
if pn contains the property name, obj[pn] should give you the value.
在 Firefox 和 Chrome/Safari 中,您可以简单地使用...
控制台将输出类似“对象”的内容
。如果您在 Chrome 中,您可以使用内置的开发人员控制台轻松检查对象的内部值。
如果你使用 firefox,输出也应该来自 firebug...
我将其声明为使用 document.write 的替代方案,因为在文档上输出内容对我来说似乎有点侵入性...
Well in firefox and in Chrome/Safari you could simply use...
And the console will output something like "Object"
If you are in Chrome you could inspect the internal values of the object with ease using the built in developer console.
If you use firefox the output should come out of firebug as well...
I'm stating this as an alternative of using document.write as it seems a little bit invasive to me to output content on the document...
编辑:
您使用了两个不同的变量名称:flash
和flashcards
。我不知道它们是否意味着相同的东西,但是您正在使用[]
表示法设置值,然后得到 它使用.
表示法。尝试:
如果
term
是一个变量来表示您要检索的属性,那么您应该使用方括号表示法从对象中获取属性。示例: http://jsfiddle.net/xbMjc/ (使用警报而不是 document.write)
EDIT:
You're using two different variable names,you are setting the value using theflash
andflashcards
. I don't know if they are meant to be the same thing, but[]
notation, then getting it using.
notation.Try:
If
term
is a variable to represent the property you are retrieving, then you should use the square bracket notation for getting the property from the object.Example: http://jsfiddle.net/xbMjc/ (uses alerts instead of document.write)