tags:

views:

6870

answers:

3

We need to see what methods/fields an object has in Javascript.

+7  A: 

If you are using firefox then the firebug plug-in console is an excellent way of examining objects

console.debug(myObject);

Alternatively you can loop through the properties (including methods) like this:

for (property in object) {
    // do what you want with property, object[property].value
}
Ken
+1  A: 

If you use Firebug, you can use console.log to output an object and get a hyperlinked, explorable item in the console.

Paul Dixon
+13  A: 

As the others said, you can use Firebug, and that will sort you out no worries on Firefox. For other browsers, there's Firebug Lite.

If Firebug isn't an option for you, then try this simple script:

function dump(obj) {
    var out = '';
    for (var i in obj) {
        out += i + ": " + obj[i] + "\n";
    }

    alert(out);

    // or, if you wanted to avoid alerts...

    var pre = document.createElement('pre');
    pre.innerHTML = out;
    document.body.appendChild(pre)
}

I'd recommend against alerting each individual property: some objects have a LOT of properties and you'll be there all day clicking "OK", "OK", "OK", "O... dammit that was the property I was looking for".

nickf
I'd recommend against it as well - frankly I'd just use console.debug. But I was pointing out the possibility of looping - it is up to the user what they want to do with each property
Ken
I've been using firebug for a while now, but wasn't aware of Firebug Lite, thanks for pointing it out.
codefin
Nice snippet. +1
Atømix