Object.prototype.inspect ?

Garrett Smith dhtmlkitchen at gmail.com
Wed Mar 11 10:33:57 PDT 2009


On Wed, Mar 11, 2009 at 9:42 AM, Brendan Eich <brendan at mozilla.com> wrote:
> On Mar 11, 2009, at 4:20 AM, Tobie Langel wrote:
>
>> It is very useful to be able to specify the debugging representation of an
>> object distinctively from it's toJSON or toString representation.
>>
>> We've been using that to great effect in Prototype, to help out, for
>> example, with the debugging representation of DOM Elements:
>>
>>   <div id="foo" class="foo bar">
>>
>> is a much more useful representation than:
>>
>>   [object HTMLDivElement]
>
>
> True, although you have to ampersand-escape it if it flows into markup.
>
>
>> 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.
>
> What text in the link you give made you think anything of the sort? I see no
> use of "internally" or "internal" in that wiki page. You can override
> toSource just as you can override toString. Doing so in a content window
> cannot possibly affect the operation of the browser's user interface
> ("chrome") windows.
>

I prefer to use the term "shadow" to override. In the example below,
C.prototype.toSource shadows Object.prototype.toSource;

function C(){
  this.name = "c1";
}

C.prototype = {
  name : "c",
  title : "a C object",
  toSource : function() {
    return this.name + "::C\n" +
    Object.prototype.toSource.call(this);
  }
};

Result:
c1::C
({name:"c1"})

Perhaps you would like to use a for-in loop to debug properties;
for(var p in this) {
  // build a string.
}

?
Garrett

> /be
>
>>
>>
>> Best,
>>
>> Tobie
>>
>> [1]
>> https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/toSource
>>
>>
>> On Mar 11, 2009, at 00:01 , Jason Orendorff wrote:
>>
>>> On Sat, Mar 7, 2009 at 9:18 PM, Tobie Langel <tobie.langel at gmail.com>
>>> wrote:
>>>>
>>>> The lack of a more developer-friendly representation of objects than the
>>>> one
>>>> obtained through their toString method (or by calling
>>>> Object.prototype.toString) made me long for an ES equivalent of Ruby's
>>>> inspect instance method.
>>>
>>> You could use JSON.stringify(obj), which is on track to be
>>> standardized in ES3.1.
>>>
>>> (SpiderMonkey has the nonstandard uneval, as well as
>>> Object.prototype.toSource, which I think are a bit closer to what you
>>> want.)
>>>
>>> -j
>>
>> _______________________________________________
>> Es-discuss mailing list
>> Es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>
> _______________________________________________
> Es-discuss mailing list
> Es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>


More information about the Es-discuss mailing list