What's the equivalent of this in Javascript? [closed]
print dir(someOjbect)
How can I print that in console?
You can install Firebug and do
console.log(someObject);
If you're using Chrome, just open up your JavaScript Console or Developer Tools.
var red = {color: 'red'};
console.log(red);
The quickest and dirtiest way is alert(someObject)
, but it does not help much with objects. You can write a crude dir()
like this:
function dir(obj) {
var s = '(';
for (k in obj) {
v = obj[k];
if (typeof v != 'function') s += ""+k+":"+v", ";
}
return s+")";
}
Something like this?
console.debug(someOjbect);
There's not a 100% equivalent in JS. However, if you use Firebug's console.log(someObj), you'll see an object in the console that you can click to see its properties.
Alternatively, you can encode and object as json, which will now show any functions that are properties of the object. https://github.com/douglascrockford/JSON-js
To print in 'console', you are talking about something browser specific. With Firebug and Web Inspector, you can use console.log(stuff)
to write to the console. If you are actually trying to append to the HTML body, use document.write(stuff)
or document.writeln(stuff)
Each browser has it's own internal console. If you're using Firefox and have Firebug installed and enabled, you should be able to do something like:
console.log(someObject);
Depending on how many objects you need to do this for, this works just fine too:
alert(someObject);
console.dir(object)
object works fine in Firebug and JS inspectors derived from it.
Beware that not all interpreters support console
though, so the usual idiom is:
if (typeof console !== 'undefined') {
console.dir(object);
}
From http://getfirebug.com/logging :
Object inspection
How many times have you hand-written code to dump all of the properties of an object, or all of the elements in an HTML fragment? With Firebug, you'll never write that code again.
Calling console.dir(object) will log an interactive listing of an object's properties, like a miniature version of the DOM tab. Calling console.dirxml(element) on any HTML or XML element will print a lovely XML outline, like a miniature version of the HTML tab.
精彩评论