获取有关 Javascript 变量的所有信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5297574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting all info on a Javascript variable
提问by KdgDev
I'm not a JavaScript Wizard by a long shot. But I am a web-developer and so I need to know my way around it at least a bit.
从长远来看,我不是 JavaScript 向导。但我是一名网络开发人员,所以我至少需要了解一下我的方法。
Something I'll often do is simply alert a variable to see what it is.
我经常做的事情只是提醒一个变量看看它是什么。
However, the problem is that I'll often get a result like: "object HTMLInputElement". To me this means little to nothing. Sure I can look it up, but I need to alert children() of children() of children(), etc...
然而,问题是我经常会得到这样的结果:“object HTMLInputElement”。对我来说,这几乎没有任何意义。当然我可以查一下,但是我需要提醒 children() of children() of children() 等等......
I've tried walking through the JavaScript with Firebug, but for some reason this is veryslow. Firefox hangs when I start a debug session, for every single debug session and I don't know why.
我试过用 Firebug 浏览 JavaScript,但由于某种原因,这很慢。当我开始调试会话时,Firefox 挂起,对于每个调试会话,我不知道为什么。
So, I want to inform if there is a way to get detailed info on variables some other way. Or a system I can use to work with to make things easier.
所以,我想告知是否有办法以其他方式获取有关变量的详细信息。或者我可以用来使事情变得更容易的系统。
采纳答案by T.J. Crowder
I find the developer tools in Chrome work quite well, giving a good amount of detail on demand (usually just hovering the mouse over the variable in the script tab; if that variable is a structured object, a little tree control appears and you can drill down). But then, I don't have your Firebug issue either (or at least, not often anymore).
我发现 Chrome 中的开发人员工具运行良好,按需提供大量细节(通常只需将鼠标悬停在脚本选项卡中的变量上;如果该变量是结构化对象,则会出现一个小树控件,您可以钻取向下)。但是,我也没有你的 Firebug 问题(或者至少,不再经常出现)。
Debugging with alertis very time-wasteful and, as you've found, frustrating; if at all possible I'd look at using a real debugger (like Chrome's Dev Tools; I've also heard good things about Opera's).
调试alert非常浪费时间,而且如您所见,令人沮丧;如果可能的话,我会考虑使用真正的调试器(比如 Chrome 的开发工具;我也听说过关于 Opera 的好东西)。
回答by Richard H
The easiest way to inspect a javascript variable is with a debugger. If Firebug is not working out for you try using Google Chrome, it has an inspector built in.
检查 javascript 变量的最简单方法是使用调试器。如果 Firebug 不适合您尝试使用 Google Chrome,它有一个内置的检查器。
Btw - not sure what you mean by "start a debug session". If you have firebug installed, you should simply be able to click on the firebug icon on the bottom right of your browser. Go to the script tab, and select from the drop down whatever js file you want, stick in a break point (just left-click on the margin) and refresh the page. I've never had a problem with Firebug, it's always worked extremely well. I strongly advise figuring out whatever your issue with it is, it will make your life a million times easier.
顺便说一句 - 不确定“启动调试会话”是什么意思。如果您安装了萤火虫,您应该只需单击浏览器右下角的萤火虫图标即可。转到脚本选项卡,从下拉列表中选择您想要的任何 js 文件,停留在断点处(只需左键单击边距)并刷新页面。我从来没有遇到过 Firebug 的问题,它总是运行得非常好。我强烈建议弄清楚你的问题是什么,它会让你的生活轻松一百万倍。
回答by bezmax
回答by roryf
Using any of the browser dev tools, including IE9, you can use console.logto get the variable output on the console. What information this gives you will vary by browser, but with Firebug it allows you to explore the variable in the DOM inspector tab, with full access to all properties and functions (you can even copy the content of a function to paste elsewhere).
使用任何浏览器开发工具,包括 IE9,您都可以console.log在控制台上获取变量输出。这给您提供的信息因浏览器而异,但使用 Firebug,它允许您浏览 DOM 检查器选项卡中的变量,并可以完全访问所有属性和函数(您甚至可以复制函数的内容以粘贴到其他地方)。
For instance:
例如:
var foo = {};
// Do lots of stuff with foo
if (typeof(console) !== "undefined" && console.log) { // prevent errors when console not open, ideally all console calls should be removed before going into production
console.log(foo);
}
This has the advantage that it doesn't require any break points, so no slow step-debugging. It won't solve everything though, you'll often still need the debugger.
这样做的优点是不需要任何断点,因此不会进行缓慢的单步调试。虽然它不会解决所有问题,但您通常仍然需要调试器。

