Object.prototype.inspect ?

Jason Orendorff jason.orendorff at gmail.com
Thu Mar 12 11:28:08 PDT 2009


Apologies, I thought I sent this yesterday but never got around to it:

On Wed, Mar 11, 2009 at 6:20 AM, Tobie Langel <tobie.langel at gmail.com> wrote:
> It is very useful to be able to specify the debugging representation of an
> object distinctively from it's toJSON or toString representation.

No doubt.  I've implemented similar functionality on every serious
JavaScript project I've done.

> Indeed, Object.prototype.toSource seems closer to what I was interested in.
> Unfortunately, it's
> 1) non standardized, and
> 2) apparently used internally by FF[1].
>
> To be useful for debugging, such a method needs to provide a good default
> value and be easily overridden for custom objects. I wouldn't be too keen on
> overriding a method which the browser vendor claims to use internally.

I think you have found some wiki nonsense.  We use toSource internally
(a) when generating error messages (b) in the default toSource methods
themselves, when recursively toSourcing the contents of an Array or
Object.  I'll fix the wiki.  [UPDATE: I edited the page, as you
noticed...]

I agree this would be useful to standardize and has plenty of
precedent in other languages--in Python it's called repr()--but I
doubt anyone wants to hold up 3.1 to get it in.  There are a few
details to nail down (regarding e.g. Arrays that contain
themselves--try that in Firefox to see something else curious,
borrowed from Lisp I think...).  Perhaps in ES4?

-j


More information about the Es-discuss mailing list