class prototype object vs class instance properties

Brendan Eich brendan at
Sat Aug 18 12:58:01 PDT 2007

On Aug 18, 2007, at 10:13 AM, Garrett wrote:

> liorean wrote:
>>> What would be the benefit of a class having a prototype over  
>>> 9instance
>>> properties/methods?
>> Prototype properties can be shadowed by instance properties without
>> being changed, prototype properties are fallbacks if the instance
>> property does not exist, and prototype properties are not part of the
>> actual instance, so can be used as memory footprint reduction if one
>> has lots of instances that don't need separate values for that
>> instance property.
>> -- 
> That's pretty much how es3 works, then.

In fact the reference implementation at uses  
ES4 class definitions to create the built-in classes from ES1-3  
(Object, Date, etc.). A few fine points:

In |dynamic class Object {...}|, the |dynamic| keyword is required if  
you want the class's instances, not just its prototype, to allow ad- 
hoc properties to be set on them. So indeed, |Object| is a dynamic  
class in the reference implementation.

In |class C { var x; }; c = new C; c.x = 42| and |dynamic class D  
{ prototype var x; }; d = new D; d.x = 42| both c and d wind up with  
an x property having value 42. But c.x is a "fixture" -- it can't be  
deleted, and if it has a type annotation, that type constraint can't  
be altered. Whereas d.x is just an ad-hoc property on a dynamic  
class's instance, which happens to shadow prototype var x, but has no  
type relation to that prototype property.

A function definition inside a class C's body |class C { function m 
():void {...} ...}| creates a method fixture named m, which has a  
function structural type |function (this:C):void| and which can't be  
deleted or overwritten.

So classes, fields (var in class), and methods (function in class)  
respectively provide objects with private members where the instances  
cannot be extended, properties that can't be deleted or have their  
type annotations violated, and receiver-based method dispatch (again  
with existence and type guarantees). These can be emulated in ES3 in  
several ways, at some cost in clarity and runtime efficiency.


More information about the Es4-discuss mailing list