Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
116
rated 0 times [  118] [ 2]  / answers: 1 / hits: 127031  / 13 Years ago, fri, june 10, 2011, 12:00:00

When I console.log() an object in my JavaScript program, I just see the output [object Object], which is not very helpful in figuring out what object (or even what type of object) it is.



In C# I'm used to overriding ToString() to be able to customize the debugger representation of an object. Is there anything similar I can do in JavaScript?


More From » debugging

 Answers
21

You can override toString in Javascript as well. See example:




function Foo() {}

// toString override added to prototype of Foo class
Foo.prototype.toString = function() {
return [object Foo];
}

var f = new Foo();
console.log( + f); // console displays [object Foo]




See this discussion on how to determine object type name in JavaScript.


[#91769] Thursday, June 9, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jarvisjovannia

Total Points: 127
Total Questions: 123
Total Answers: 101

Location: Netherlands
Member since Mon, Jun 7, 2021
3 Years ago
;