Analog to Object.getPropertyDescriptor() for *changing* a property value?

Axel Rauschmayer axel at rauschma.de
Wed Jun 22 15:55:20 PDT 2011


> You should not in general mutate shared prototype state via one of N delegating objects. It leads to trouble.

What’s the difference to a global variable? I always thought about an instance in JavaScript as being composed of a non-shared object and a shared meta-object. Why should the former be able to change its state, but not the latter? Is there a fundamental difference between object and meta-object in this case?

>> The prototype seems to be the best location for shared data (such as the number of instances of a class).
> 
> Counting instances can't be done without leaking GC non-determinsm, so this is a bogus example. Can you come up with another? Shared mutable state in prototypes that is visibly mutable by means of a reference to one of N delegating objects  is usually a bug.


One thing I did in pre-enum days in Java was to define constants and collect all values at the same time:

const red = new Color("FF0000");
const green = new Color("00FF00");
Color.prototype.getAllColors().forEach(...);

But you could also produce the colors with a factory.

-- 
Dr. Axel Rauschmayer

axel at rauschma.de
twitter.com/rauschma

home: rauschma.de
blog: 2ality.com





More information about the es-discuss mailing list