javascript - What property is alert showing me? - Stack Overflow

When I am writing Javascript, I use window.alert() to debug. Usually I pass variables to alert and it p

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!

z.

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!

z.

Share Improve this question asked Jan 17, 2010 at 23:44 ZiggyZiggy 22.5k28 gold badges79 silver badges110 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

You must overwrite the toString() method. Otherwise, Object.prototype.toString will be used, which returns "[object Foo]", where Foo is the value of the internal (ie inaccessible) class property of the object.

Try redefining "toString()" of your class prototype: it's used to create a string type of an object. Default it's [object <type>], but that's not what you want, is it.

Though, "You can override this method for custom objects that you create. If you do not override toString in a custom object, toString returns [object type], where type is the object type or the name of the constructor function that created the object.", http://www.synchro/docs/js/ref/object.html#1193350

Instead of debugging with alert(), you should look at Firebug, and outstanding web development tool, with better HTML inspection, javascript debugging, and other goodies.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745633517a4637249.html

相关推荐

  • javascript - What property is alert showing me? - Stack Overflow

    When I am writing Javascript, I use window.alert() to debug. Usually I pass variables to alert and it p

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信