On Fri, Nov 30, 2012 at 7:40 PM, Allen Wirfs-Brock <[email protected]>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.

-j
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to