Object.prototype.inspect ?

Tobie Langel tobie.langel at gmail.com
Thu Mar 12 18:45:24 PDT 2009

On Mar 13, 2009, at 00:27 , Brendan Eich wrote:

> So a string-valued property, e.g. f.constructor.name === "Function"  
> for function f(){}, d.constructor.name === "Date" for (d = new  
> Date), etc.?

Yes. That would also help solve cross-frame issues for which  
instanceof Foo and constructor === Foo are useless.

> Works for me, already implemented in SpiderMonkey. Note constructor  
> being writable and configurable as I mentioned in reply to Tucker  
> just now. If we want to avoid that hazard, we would need a separate,  
> optionally locked-down method, say Object.getClassName(o).

Right, that makes sense.

Current behaviour in Firebug:

 >>> function Parent() {}
 >>> var parent = new Parent();
 >>> parent.constructor;
 >>> Object.prototype.toString.call(parent);
"[object Object]"

 >>> function Child() {}
 >>> Child.prototype = new Parent();
 >>> var child = new Child();
 >>> child.constructor;
 >>> Object.prototype.toString.call(child);
"[object Object]"

Desired behaviour:

 >>> Object.prototype.toString.call(child);
"[object Child]"
 >>> Object.prototype.toString.call(parent);
"[object Parent]"

And ideally:
 >>> Child.name;
 >>> Parent.name;

A very naïve specification would boil down to modifying step 2 and  
inserting a step 3 inside of 13.2.2 of the 23feb09 draft:

13.2.2 [[Construct]]
When the [[Construct]] property for a Function object F is called with  
a possibly empty list of
arguments, the following steps are taken:
1. Let obj be a newly created native ECMAScript object.
2. if the name property of F is a string value set the [[Class]]  
internal property of obj to the name property of F.
3. if the name property of F is not a string value set the [[Class]]  
internal property of obj to "Object".

There's probably a lot of issues I haven't foreseen here (on top of  
the yet unspecified name property of functions), but it hopefully  
helps to clarify what I was thinking about. It also avoids touching  
the behaviour of the constructor property of the prototype object.

Given that spec change, I suspect Object.getClassName(o) could be  
desugared to Object.prototype.toString.call(object).slice(8, -1).



More information about the Es-discuss mailing list