Which property shows me?

When I write Javascript, I use window.alert () for debugging. Usually I pass in variables for warning, and a pop-up line appears containing this value of the variables. However, if I pass a warning about an object, it tells me the type of object. For instance:

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

This bit, the part that says [blah blah object]. What is this property? I recently started creating my own objects to encapsulate the useful parts of the site I'm working on ... but when I pass my own objects for warning, it gives me a general message [object Object], which is not very useful if I have done a bunch of different types of objects. I would prefer, for example, my object to return to me something more along the lines of [object My_Object].

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

Thank!

g.

+3
source share
3 answers

Try overriding the "toString ()" of your class prototype: it used to create the string type of the object. By default it is [object <type>], but it is not what you want, is it.

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

+4

toString(). Object.prototype.toString, "[object Foo]", Foo - (.. ) .

+6

Instead of debugging with alert (), you should look at Firebug and an outstanding web development tool with better HTML validation, javascript debugging and other useful properties.

+2
source

Source: https://habr.com/ru/post/1728859/


All Articles