alert() return different from console.log()?

馋奶兔 提交于 2019-12-02 06:06:20

问题


Should I be using alert() for debugging; or is there a time to use alert() vs. console.log()?

I see that alert() and console.log() can return different results. I assumed that they were similar functions but just returned in different places.

Back story: my boss likes to see alerts() during development but I can't get the object details in the alert (at least not easily).

But when I run the same request through console.log, I get the object and all of it's parameters.


回答1:


Since alert could be shown to users, it tends to be literal-minded (just using toString) so a developer has a lot of control over what's shown to the user. Unlike alert, console is designed for developers, and thus tends to try to interpret a call so as to provide information that a developer would find useful: e.g. "[2, 3, 4]" is a lot more useful to a developer than "[object Object]". Alert should be the same in every browser; console's behavior could vary from browser to browser (including not being supported at all, as in IE).




回答2:


alert() converts the object passed to it into a string using the object's toString() method. Unlike alert(), console.log() is not limited to displaying a simple string and can allow you to interact with the object passed to it, for example letting you inspect its properties.




回答3:


try alert(JSON.stringify(yourObject)); (if your browser have json.stringify....)



来源:https://stackoverflow.com/questions/3909006/alert-return-different-from-console-log

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!