By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,843 Members | 2,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,843 IT Pros & Developers. It's quick & easy.

How to print object attributes/values

P: n/a
Hi,

I have this alert statement

alert(myObj);

which when executed, alerts "[object Object]", or something similar.
Is there a way I can see a list of attributes and values instead or do
I have to write a custom method for my object?

Thanks, - Dave
Jul 16 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
laredotornado schreef:
Hi,

I have this alert statement

alert(myObj);

which when executed, alerts "[object Object]", or something similar.
Is there a way I can see a list of attributes and values instead or do
I have to write a custom method for my object?

Thanks, - Dave
Hi,

A rude approach is:

for (myKey in myObj){
alert ("myObj["+myKey +"] = "+myObj[myKey]);
}

(Not tested)

Regards,
Erwin Moller
Jul 16 '08 #2

P: n/a
laredotornado wrote:
I have this alert statement

alert(myObj);

which when executed, alerts "[object Object]", or something similar.
Is there a way I can see a list of attributes and values instead or do
I have to write a custom method for my object?
There is a way to see a list of enumerable properties and their values, and
a custom method would generalize this.

<http://www.jibbering.com/faq/#FAQ2_3>
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Jul 16 '08 #3

P: n/a
On Jul 16, 4:57*pm, laredotornado <laredotorn...@zipmail.comwrote:
Hi,

I have this alert statement

alert(myObj);

which when executed, alerts "[object Object]", or something similar.
Is there a way I can see a list of attributes and values instead or do
I have to write a custom method for my object?
Yes there is, in FireFox : alert(myObj.toSource());

javascript:alert({a:1,b:2}.toSource());

--Jorge.
Jul 16 '08 #4

P: n/a
Jorge wrote:
On Jul 16, 4:57 pm, laredotornado <laredotorn...@zipmail.comwrote:
>I have this alert statement

alert(myObj);

which when executed, alerts "[object Object]", or something similar.
Is there a way I can see a list of attributes and values instead or do
I have to write a custom method for my object?

Yes there is, in FireFox : alert(myObj.toSource());
It is supported in all Gecko-based UAs because it is a JavaScript[tm]
feature. The Matrix got you!
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Jul 16 '08 #5

P: n/a
laredotornado <la***********@zipmail.comwrites:
Hi,

I have this alert statement

alert(myObj);

which when executed, alerts "[object Object]", or something similar.
Is there a way I can see a list of attributes and values instead or do
I have to write a custom method for my object?

Thanks, - Dave
Aside from the already given answers, my preferred method when using
browser-based javascript, is to use firefox's firebug extension and
call console.debug(myObj);

That will show a show summary in firebug's console/log tab, which you
can click to browse through the object including its properties. Very
handy.

See http://getfirebug.com/
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jul 16 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.