3

Consider:

var o = { a: 1, b: 2, toString: function() { return "foo"; } };

In Chrome dev tools:

Debug Screenshot

Is there anything I could do to the object such that o is displayed in the debug console as "foo" instead of the full object?

5
  • .toString() is not good enough? I mean, if you want the return value of o.toString the easiest and cleanest way is calling it as you've done. Commented Aug 21, 2013 at 22:29
  • By comparison, in .NET, overriding ToString() will use it implicitly when debugging, even when you don't call .ToString(). I want to know if the same effect is possible in JavaScript somehow. Commented Aug 21, 2013 at 22:32
  • You could easily overload console.log, but I'm thinking about the side-effects. Commented Aug 21, 2013 at 22:33
  • 2
    Well you could cast it ''+o Commented Aug 21, 2013 at 22:33
  • @elclanrs - thanks, that's close - but I want a way to put this inside the object so it is cast implicitly. Commented Aug 21, 2013 at 23:28

1 Answer 1

2

Here's my attempt:

(function() {
    var cl = console.log;
    console.log = function() {
        cl.apply(console, [].slice.call(arguments).map(function(el) {
            return {}.toString.call(el) === '[object Object]' && typeof el.toString === 'function' && el.toString !== Object.prototype.toString ? el.toString() : el;
        }));
    };
}());

^ Just throw this script before any console.log call.


Test case:

console.log('str', 42, /rege?x/, { a: 1 }, [1, 2], {
        toString: function() { return "foo"; }
    }, new function() {
        this.toString = function() {
            return 'bar';
        };
    }
);

console.log output


This just maps plain/constructed objects which have a toString method different from Object.prototype.toString to their .toString() value. I chose this way over hasOwnProperty as constructors may also have a toString method in their prototype.

As you can see, all objects and even primitives inherit a toString method from the native constructors, so you may need tweaking for specific use cases. The snippet above does not stringify function objects with custom toString properties, for example.

Sign up to request clarification or add additional context in comments.

8 Comments

This should suffice for all common use cases that I can think of. Feel free to comment if you manage to find any bugs with it.
Nice attempt, this is pretty close - except you still have to call console.log(o) instead of just o. I guess the debug window doesn't go through console.log directly?
Also, I was really hoping for something I could put inside the object so it was wired up but wouldn't affect everything globally. Perhaps that is just not possible in JS?
@MattJohnson Oh I see, thought you were referring to console.log which is often used from inside the code, as for entering it directly on Dev Tools it can't be solved with plain JS I believe. Maybe it is possible to extend or override certain parts of the Dev Tools (I've seen extensions that extend the Firebug console but not sure if there's an API to extend the Dev Tools console).
This may be possible with chrome.experimental.devtools.console, I'll check whether I can get something useful out of it. =]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.