Nate Pet
Nate Pet

Reputation: 46222

javascript Object

When I do the following:

    alert(objecr);

It shows as object Object

How do I display the content of what is in the object? I also tried

    alert(JSON.stringify(objecr));

but it shows the following:

"[object Object]"

Upvotes: -1

Views: 104

Answers (4)

Chris Sobolewski
Chris Sobolewski

Reputation: 12925

Assuming you're using a modern browser to debug, don't use alerts.

console.log(objecr);

Then look in your debug console. IE9, Chrome, FF, and Opera all have good consoles for viewing objects. I imagine Safari does as well.

Upvotes: 2

antonjs
antonjs

Reputation: 14318

Another possibility is to read your object in this way:

for (var key in obj) {
    if (obj.hasOwnProperty(key)) {
        /* useful code here */
    }
}

In your case:

for (var key in objecr) {
    if (objecr.hasOwnProperty(key)) {
        alert(objecr[key]);
    }
}

Upvotes: 0

fanaugen
fanaugen

Reputation: 1127

I just tested: in Chrome, both obj.toString() and JSON.stringify(obj) return a string showing the object's property keys and values. This can then be logged on the console or fed to an alert()...

Upvotes: 0

Peter Aron Zentai
Peter Aron Zentai

Reputation: 11740

try console.dir(object) and check out the script console. it will reflect through the object instance (works best in WebKit based browsers).

Other ways could be getting the member list with Object.getOwnPropertyNames and Object.keys invoking on the instance and also on its prototype chain (Object.getPrototypeOf(object))..

Upvotes: 0

Related Questions