tags:

views:

1379

answers:

8

Typically if we just use alert(object) it will be show as "element object". How to print all the content parameters of an object in JavaScript?

+2  A: 

You should consider using FireBug for JavaScript debugging. It will let you interactively inspect all of your variables, and even step through functions.

Jonathan Feinberg
A: 

Use dir(object). Or you can always download Firebug for Firefox (really helpful).

Lex
dir? need include any special js file?
cometta
its only needs the firebug extension
eskimoblood
A: 

You can also use Prototype's Object.inspect() method, which "Returns the debug-oriented string representation of the object".

http://api.prototypejs.org/language/object.html#inspect-class%5Fmethod

snw
Its quite unhelpful for a javascript library to call itself after a fundamental aspect of a language.
Toby Allen
+3  A: 

If you just want to have a string representation of an object, you could use the JSON.stringify function, using a JSON library.

CMS
+3  A: 

Aside from using a debugger, you can also access all elements of an object using a foreach loop. The following printObject function should alert() your object showing all properties and respective values.

function printObject(o) {
  var out = '';
  for (var p in o) {
    out += p + ': ' + o[p] + '\n';
  }
  alert(out);
}

// now test it:
var myObject = {'something': 1, 'other thing': 2};
printObject(myObject);

Using a DOM inspection tool is preferable because it allows you to dig under the properties that are objects themselves. Firefox has FireBug but all other major browsers (IE, Chrome, Safari) also have debugging tools built-in that you should check.

Miguel Ventura
Might want to add if (o.hasOwnProperty(p)) inside the loop
Mike Blandford
That's a good idea, but when debugging I'd rather see it all. Perhaps even better would be something like:for (var p in o) { if (!o.hasOwnProperty(p)) out += '(inherited) '; out += p + ': ' + o[p] + '\n';}
Miguel Ventura
A: 

Internet Explorer 8 has developer tools which is similar to Firebug for Firefox. Opera has Opera DragonFly, and Google Chrome also has something called Developer Tools (Shift+Ctrl+J).

Here is more a more detailed answer to debug JavaScript in IE6-8: http://stackoverflow.com/questions/780059/using-the-ie8-developer-tools-to-debug-earlier-ie-versions/801547#801547

gregers
+5  A: 

If you are using Firefox, alert(object.toSource()) should suffice for simple debugging purposes.

Lukman
A: 

You can give your objects their own toString methods in their prototypes.

kennebec