Object.prototype.inspect ?

Brendan Eich brendan at mozilla.com
Thu Mar 12 18:38:36 PDT 2009


On Mar 12, 2009, at 6:30 PM, Breton Slivka wrote:

> So is there a reason the workaround is intolerable? You could go
> further with this workaround in es3.1 and define it as a readonly
> property, and also shadow its toSource to output a call to the
> constructor function, with appropriate values. This puts the
> responsibility for the housekeeping on those who care about the
> accuracy of the constructor, without burdening those of us who use
> strategies that don't depend on the value being accurate.

That's the rationale in a nutshell for the de minimus ES1 (pre-ES1, I  
had it in Netscape 3 if not 2) setting of f.prototype.constructor to f  
when creating f.prototype on demand.

It could be argued it's too implemnetation-oriented but we had a hard  
time figuring out how to elaborate it without imposing a per-instance  
cost (as Tucker chose to do, but that was his choice) on all objects.  
I remember talking through this in ES1 days, early 1997 probably.


> As long as we're talking roundtripping, might I just point out that
> creating a full roundtripping toSource() for a function value is a
> minefield? As has already been pointed out, there's the issues with
> callable host objects, and private data. But let's not forget
> closures, and enumerable (or even non enumerable) function properties.
> functions are objects too! I cannot think of a particularly tidy way
> to serialize either of those into a single expression.

No one is trying for this. It's an anti-goal.


> What about arrays with defined properties? You can convert them to the
> object notation, but how do you deserialize it to have the array
> prototype? (well, you could have __proto__ defined I suppose)

Back in ES3 era we played with a generalized object initialiser syntax  
that allowed you to specify the class constructor. Syntax proposals  
circled around the low-energy well close to Java's anonymous inner  
classes:

var arrayWithNamedProps = new Array {0:a, 1:b, 2:c, foo:"hi", bar:42};

but automatic semicolon insertion bites this hard.


> It would be nice if this all worked, and there was syntax for it, but
> I have the feeling that roundtripping has got to be a non goal. the
> tc39 crew may have time for one or two improvements, but going all the
> way is a big-  maybe even impossible job.

Yes, and on this point Rok Yu was right. But it was the wrong goal.  
Can we have an inspect-like facility, as Tobie requests, without  
sliding down this slope? I think so.

/be
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20090312/6371d87f/attachment.html>


More information about the Es-discuss mailing list