Sunday, January 17, 2010

What property is alert showing me?

Hi

When I am writing Javascript, I use window.alert() to debug. Usually I pass variables to alert and it pops up with a string containing that variables value. However, if I pass alert an object, it tells me the type of the object. For example:


var form = document.getElementById("my_form");
alert(form); // returns [object HTMLFormElement]

That bit, the part that says [object blah blah]. What property is that? I have recently started making my own objects to encapsulate useful parts of the site I am working on... but when I pass my own objects to alert it gives me the generic message [object Object], which is not very helpful if I have made a bunch of different kinds of object. I would prefer, for instance, my object to return to me something more along the lines of [object My_Object].

Is there a property I can set in function My_Object() that will tell alert what I want it to tell me?

Thanks!

No comments:

Post a Comment