PHP 中 var_dump 或 print_r 在 JavaScript 中的等价物是什么?
我想查看 JavaScript 中对象的结构(用于调试)。 PHP中有类似var_dump的东西吗?
I would like to see the structure of object in JavaScript (for debugging). Is there anything similar to var_dump in PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
大多数现代浏览器的开发工具中都有一个控制台,对于此类调试很有用。
然后,您将在控制台中获得一个精心映射的对象/任何内容的界面。
查看
控制台
文档更多细节。Most modern browsers have a console in their developer tools, useful for this sort of debugging.
Then you will get a nicely mapped out interface of the object/whatever in the console.
Check out the
console
documentation for more details.最常见的方法:
但是我必须提到
JSON.stringify
这对于转储非浏览器脚本中的变量很有用:JSON.stringify
函数还支持内置的美化功能作者:Simon Zyx。示例:
上面的代码片段将打印:
在 caniuse.com 上,您可以查看原生支持
JSON.stringify
函数的浏览器:http://caniuse.com/json您还可以使用 Douglas Crockford 库在旧浏览器上添加
JSON.stringify
支持:< a href="https://github.com/douglascrockford/JSON-js" rel="noreferrer">https://github.com/douglascrockford/JSON-jsJSON.stringify< 的文档/code>: https://developer.mozilla .org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
我希望这有帮助:-)
Most common way:
However I must mention
JSON.stringify
which is useful to dump variables in non-browser scripts:The
JSON.stringify
function also supports built-in prettification as pointed out by Simon Zyx.Example:
The above snippet will print:
On caniuse.com you can view the browsers that support natively the
JSON.stringify
function: http://caniuse.com/jsonYou can also use the Douglas Crockford library to add
JSON.stringify
support on old browsers: https://github.com/douglascrockford/JSON-jsDocs for
JSON.stringify
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringifyI hope this helps :-)
我编写了这个 JS 函数
dump()
,使其像 PHP 的var_dump()
一样工作。要在警报窗口中显示变量的内容:
dump(variable)
在网页中显示变量的内容:
dump(variable, 'body')
仅获取变量的字符串:
dump(variable, 'none')
I wrote this JS function
dump()
to work like PHP'svar_dump()
.To show the contents of the variable in an alert window:
dump(variable)
To show the contents of the variable in the web page:
dump(variable, 'body')
To just get a string of the variable:
dump(variable, 'none')
JavaScript 中的 var_dump 等效项? 很简单,没有一个。
但是,这并不意味着您无助。 就像一些人建议的那样,使用 Firebug (或其他浏览器中的等效项),但与其他人建议的不同,不要使用控制台.log 当你有一个(稍微)更好的工具 console.dir< /a>:
The var_dump equivalent in JavaScript? Simply, there isn't one.
But, that doesn't mean you're left helpless. Like some have suggested, use Firebug (or equivalent in other browsers), but unlike what others suggested, don't use console.log when you have a (slightly) better tool console.dir:
正如其他人已经提到的,调试变量的最佳方法是使用现代浏览器的开发人员控制台(例如 Chrome 开发人员工具< /a>、Firefox+Firebug、Opera Dragonfly > (现在在新的基于 Chromium 的 (Blink) Opera 中消失了,但作为开发人员 说,“尽管我们还无法向您提供更多信息,但 Dragonfly 并未消亡”)。
但是,如果您需要其他方法,有一个非常有用的网站,名为
php.js:
http://phpjs.org/
提供 “PHP 函数的 JavaScript 替代品” - 因此您可以像在 PHP 中一样使用它们。 我将在此处将适当的函数复制粘贴给您,但是请注意,如果检测到某些错误,这些代码可以在原始站点上更新,因此我建议您访问 phpjs.org 网站! (顺便说一句。我不隶属于该网站,但我发现它非常有用。)
JavaScript 中的
var_dump()
下面是
var_dump()
的 JS 替代代码代码>:http://phpjs.org/functions/var_dump/
它取决于
echo()
函数:http://phpjs.org/functions/echo/JavaScript 中的
print_r()
下面是
print_r()
函数:http://phpjs.org/functions/print_r/
它也取决于
echo()
。JavaScript 中的
var_export()
您可能还会发现
var_export()
替代方案很有用,它也依赖于echo()
:JavaScript 中的http://phpjs.org/functions/var_export/
echo()
http://phpjs.org/functions/echo/
As others have already mentioned, the best way to debug your variables is to use a modern browser's developer console (e.g. Chrome Developer Tools, Firefox+Firebug, Opera Dragonfly (which now disappeared in the new Chromium-based (Blink) Opera, but as developers say, "Dragonfly is not dead though we cannot give you more information yet").
But in case you need another approach, there's a really useful site called
php.js:
http://phpjs.org/
which provides "JavaScript alternatives to PHP functions" - so you can use them the similar way as you would in PHP. I will copy-paste the appropriate functions to you here, BUT be aware that these codes can get updated on the original site in case some bugs are detected, so I suggest you visiting the phpjs.org site! (Btw. I'm NOT affiliated with the site, but I find it extremely useful.)
var_dump()
in JavaScriptHere is the code of the JS-alternative of
var_dump()
:http://phpjs.org/functions/var_dump/
it depends on the
echo()
function: http://phpjs.org/functions/echo/print_r()
in JavaScriptHere is the
print_r()
function:http://phpjs.org/functions/print_r/
It depends on
echo()
too.var_export()
in JavaScriptYou may also find the
var_export()
alternative useful, which also depends onecho()
:http://phpjs.org/functions/var_export/
echo()
in JavaScripthttp://phpjs.org/functions/echo/
Firebug。
然后,在您的 javascript 中:
现在您可以查看控制台,单击该语句并查看里面的内容
blah
Firebug.
Then, in your javascript:
Now you can look at the console, click on the statement and see what is inside
blah
一个很好的简单解决方案,用于解析 HTML 的 JSON 响应。
A nice simple solution for parsing a JSON Response to HTML.
你也可以试试这个功能。 我不记得原作者了,但所有的功劳都归他们所有。
工作起来就像一个魅力 - 与 PHP 中的 var_dump 100% 相同。
一探究竟。
You could also try this function. I can't remember the original author, but all credits go to them.
Works like a charm - 100% the same as var_dump in PHP.
Check it out.
我提出这个是为了帮助任何需要一些实用的东西的人,为您提供一个漂亮的、美化的(缩进的)JS
Node
图片。 对于节点(“循环错误”或其他什么......),其他解决方案都不适合我。 这将引导您浏览 DOMNode
下的树(不使用递归),并为您提供深度、tagName
(如果适用)和textContent
(如果适用)。您在头节点下行走树时遇到的节点的任何其他详细信息都可以根据您的兴趣添加...
I put this forward to help anyone needing something readily practical for giving you a nice, prettified (indented) picture of a JS
Node
. None of the other solutions worked for me for aNode
("cyclical error" or whatever...). This walks you through the tree under the DOMNode
(without using recursion) and gives you the depth,tagName
(if applicable) andtextContent
(if applicable).Any other details from the nodes you encounter as you walk the tree under the head node can be added as per your interest...