Object.prototype.inspect ?

Breton Slivka zen at zenpsycho.com
Thu Mar 12 18:30:36 PDT 2009

On Fri, Mar 13, 2009 at 11:14 AM, P T Withington <ptw at pobox.com> wrote:
> function subclass () { this.constructor = arguments.callee; }
> I don't want to smash mysub.prototype.constructor, because that is how I
> implement 'super': constructor.prototype.constructor.
>> I don't think we can change this, anyway -- it's an incompatible change.
>> More, since constructor can be deleted or overwritten, counting on it for
>> inspection is probably a bad idea.
> Yeah, I'm sure you are right.  And in my case, since I control the
> Javascript that is getting written, I can live with it.  But, I can't for
> the life of me think of a use case for the existing way that constructor
> works.
> _______________________________________________
> Es-discuss mailing list
> Es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

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.

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.

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)

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.

More information about the Es-discuss mailing list