Object.prototype.inspect ?

Brendan Eich brendan at mozilla.com
Thu Mar 12 16:25:26 PDT 2009


On Mar 12, 2009, at 2:23 PM, P T Withington wrote:

>> Identifying the constructor is harder. Having to slice a substring  
>> out of Object.prototype.toString.call(obj) is disgusting, but it  
>> kind of works. Not sure about all the various non-standard class  
>> names formatted that way (IE calls DOM classes "Object"?!).
>
> 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?

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.

/be


More information about the Es-discuss mailing list