-1

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]"

2
  • Where does objecr come from? Because JSON.stringify works. Commented Mar 23, 2012 at 16:34
  • If that's the case then I don't think you've got an object; you got a string containing "[object Object]". Commented Mar 23, 2012 at 16:34

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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))..

1 Comment

@fanaugen: obj.toString() by default never returns the property names. JSON.stringify will only show non-function, enumerable members (those with enumerable:false in their property descriptor will not be shown)
0

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()...

Comments

0

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]);
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.