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 开发者_JAVA百科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.
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).
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.
try alert(JSON.stringify(yourObject)); (if your browser have json.stringify....)
精彩评论