简体   繁体   English

如何读取对象文字的JavaScript数组?

[英]How to read JavaScript arrays of object literals?

In a JavaScript application that I am working on I have an array of objects: 在我正在使用的JavaScript应用程序中,我有一个对象数组:

function Message(title, message) {
    this.title=title;
    this.message=message;
}

var MessageArray = new Array();
MessageArray[0] = new Message("Some Title", "Some new message.");

I have an issue that I need to debug. 我有一个需要调试的问题。 Is there any way to read or sniff the current objects in the array, including their title and message properties? 有什么方法可以读取或嗅探数组中的当前对象,包括它们的titlemessage属性? I could add another function to alert() me with those values, but if there is a tool that I could use much like the Visual Studio debugger then I would love to know about it. 我可以使用这些值向alert()添加另一个函数,但是如果有一个可以像Visual Studio调试器一样使用的工具,那么我很想知道这一点。 I took a look at Firebug but it does not seem to have this capacity. 我看了一下Firebug,但它似乎没有这种能力。

To be clear, I am looking for a debugger that will let me inspect any variable at runtime, not specifically MessageArray at the moment. 明确地说,我正在寻找一个调试器,该调试器可以让我在运行时检查任何变量,而不是当前检查的MessageArray。 I did find Chrome's Scope Variables pane, but it seems to only list scalars, not arrays! 我确实找到了Chrome的“作用域变量”窗格,但似乎只列出了标量,而不列出数组!

Most modern browsers have a built in developers console with all the functionality of a normal debugger. 大多数现代浏览器都具有内置的开发人员控制台,该控制台具有普通调试器的所有功能。 Breakpoints and variable inspecting ect. 断点和变量检查等。 In chrome you can open it through the tools menu or by pressing f12. 在Chrome浏览器中,您可以通过工具菜单或按f12打开它。

You can also use the JavaScript Console.log(); 您还可以使用JavaScript Console.log(); to log anything you want to the developer console. 将所需的任何内容记录到开发人员控制台。 This stop you using alerts which stop JavaScript executing while the alert it up. 这会阻止您使用警报,从而在警报启动时停止执行JavaScript。

In the case console.log(MessageArray[0].title, MessageArray[0].message); 如果是console.log(MessageArray[0].title, MessageArray[0].message); would print out the title and the message to the console. 将打印出标题和消息到控制台。

EDIT: 编辑:

Here is a screen shot of the console inside chrome. 这是Chrome内部控制台的屏幕截图。 Showing me create your objects and then interacting with them via the console. 向我展示创建您的对象,然后通过控制台与它们进行交互。

在此处输入图片说明

All modern browsers ( IE 7+, Firefox, Chrome, Opera ) have debug (developer) console, so try use it as classic debugger 所有现代浏览器(IE 7 +,Firefox,Chrome,Opera)都具有调试(开发人员)控制台,因此请尝试将其用作经典调试器

IE debug console is too from Microsoft, so try to use it - and Firebug CAN do this ( try wath MessageArray - you will find it all there ) IE调试控制台也来自Microsoft,因此请尝试使用它-Firebug可以做到这一点(尝试wath MessageArray-您将在此找到所有功能)

You can refer to those properties, for example, like this: 例如,您可以参考这些属性:

MessageArray[0].title    
MessageArray[0].message

Firebug's debugger has the functionality you need. Firebug的调试器具有您所需的功能。 just take a good look at it. 看看就好了。 if you mean using IDE tool, I'm not aware of such. 如果您的意思是使用IDE工具,那我就不知道了。

In your case you can read title and message of the stored Message object like that: 在您的情况下,您可以像这样读取已存储的Message对象的标题和消息:

var title = MessageArray[0].title;
var message = MessageArray[0].message;

To get title and message of each object within array, just do: 要获取数组中每个对象的标题和消息,只需执行以下操作:

for (var i=0; i<MessageArray.length; i++){
    // here MessageArray[i].title is the current title
    // here MessageArray[i].message is the current message
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM