Object.prototype.inspect ?

P T Withington ptw at pobox.com
Thu Mar 12 17:14:48 PDT 2009


On 2009-03-12, at 19:25EDT, Brendan Eich wrote:

> On Mar 12, 2009, at 2:23 PM, P T Withington wrote:
>>
[...]
>> So, why can't we standardize the `constructor` property, that seems  
>> to exist in some implementations, to actually return the  
>> constructor of the object (and not the constructor of the object's  
>> prototype, which is useless)?
>
> The constructor property is in ES1-3.1. For a constructor function  
> F, F.prototype.constructor === F, but constructor is writable and  
> configurable.
>
> The case you are complaining about is not exactly as your words  
> describe it:
>
> js> function F(){}
> js> F.constructor
> function Function() {
>    [native code]
> }
> js> o = new F
> [object Object]
> js> o.constructor
> function F() {
> }
>
> That works as expected, F.prototype.constructor === F therefore  
> o.constructor === F (and F.constructor === Function). Can you show  
> the case (it involves two levels of prototyping, I believe) where  
> you don't get the right default behavior, and have to shadow (thanks  
> Garrett!) the prototype's constructor property?

Exactly.  If you use prototype's to implement inheritance, you say:

js> function mysuper () {};
js> function mysub () {};
js> mysub.prototype = new mysuper;
[object Object]
js> var o = new mysub;
js> o instanceof mysuper
true
js> o instanceof mysub
true
js> o.constructor
function mysuper() {
}

Not the droids I was looking for.  My work-around is to always say:

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.


More information about the Es-discuss mailing list