Prototypes as the new class declaration

Axel Rauschmayer axel at rauschma.de
Sat Jun 18 10:30:17 PDT 2011


> Now, we could have a "second protocol" as Irakli's gists show. He wires up both per-instance and "static" (ugh, I mean constructor-side) 'new' methods that bottom out by calling the constructor. This is not quite the second protocol you proposed that we agreed would be less desirable, all else equal, than a single constructor protocol. It's a layering on top, rather than an additional parallel novel protocol.

Note though that there shouldn’t be too many steps to make this happen.

I like the image you mentioned (of flipping things around):
- Current “class”: a constructor containing a prototype
- New-style class: a prototype containing a constructor

An interesting question is how much this approach would go against the expectations of existing code (and if it really has to conform %100 to them). The following measures should make the differences minimal:

- Object.getPrototypeOf(obj).constructor: should it always point to the constructor? Or is it a reference to a class construct (either old-style or new-style)? Most of the code will probably only use it as the operand of the "new" operator.

- o instanceof C: treat non-function operands differently. Then simply desugars to C.isPrototypeOf(o).

- new C(): treat non-function operands differently. After construction, things have to look similar to what they look like today. I don’t think it necessarily matters how to get there.

-- 
Dr. Axel Rauschmayer

axel at rauschma.de
twitter.com/rauschma

home: rauschma.de
blog: 2ality.com



More information about the es-discuss mailing list