(Weak){Set|Map} subclassing

Jason Orendorff jason.orendorff at gmail.com
Sat Dec 1 10:40:11 PST 2012

On Fri, Nov 30, 2012 at 7:40 PM, Allen Wirfs-Brock <allen at wirfs-brock.com>wrote:

> Overall, I like this approach. However, I don't think @@create belongs on
> the prototype object.  This make the @@create functionality for a
> particular kind of object available to anyone who gets their hands on  an
> instance object of the class.  This smells like a capability leak.

I think you're right.

> Instead, I would make @@create a property of the actual constructor
> function and I would place the default @@create on Function.prototype,
> which all functions inherit from.
> So roughly speaking, Foo.[[Constructor]](...args) would be defined as:
> 1) Let creator be Foo.[[Get]](@@create)
> 2 ) Let newObj be creator.call(foo).  //Foo is passed as the this value to
> @@create
> 3)  Let ctorResult be Foo.[[call]](newObj,args)
> 4)  If Type(ctorResult) is Object, return ctorResult
> 5) else return newObj
> The definition of the Function.prototype.@@create would be loosely
> function() {
>     return Object.create(this.prototype);
> }
> So, Map would be defined with a Map.@@create method that in sorta
> specTalk would say something like:
> 1) Let obj be a new ordinary object.
> 2) Let proto be this.[[Get]]('Prototype').
> 2) Set the [[Prototype]] of obj to proto.
> 3)  Add a [[MapData]] internal property to obj.
> 4) Return obj

All of this looks pretty great to me, certainly an improvement on what I
proposed. This is actually similar to what Python does. I wonder if it can
be simplified a bit.

One issue with this is initializing fields that we want to be immutable.
Consider subclassing something like the String wrapper class: String.@@create()
should return an object with [[PrimitiveValue]] set to... an empty string?
It gets initialized later? It's kind of gross for that field to change
observably after allocation. Python fixes this by adding another wrinkle:
it passes the constructor arguments to the allocation hook (__new__) as
well as the initialization hook (__init__). Maybe we can achieve the same
end by simplifying instead.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121201/7d0a1812/attachment.html>

More information about the es-discuss mailing list