Non-extensible WeakMaps

David Bruant bruant.d at gmail.com
Sun Jan 22 11:27:51 PST 2012


Le 22/01/2012 20:16, Tab Atkins Jr. a écrit :
> On Sun, Jan 22, 2012 at 10:28 AM, David Bruant <bruant.d at gmail.com> wrote:
>> Hi,
>>
>> In Firefox Aurora as well as in Chromium 18, running the following
>> -----
>> var wm = new WeakMap();
>> var o = {};
>>
>> Object.preventExtensions(wm);
>>
>> wm.set(o, 1);
>> console.log(wm.get(o)); // 1
>> -----
>>
>> Is this something that is wanted?
>> Same question for Maps and Sets.
> Calling set() on a WeakMap doesn't add any properties to the WeakMap
> object, so yes, it's expected that preventExtensions has no effect.
I agree that Object.preventExtensions is defined as preventing addition
of new properties. Likewise, Object.freeze and Object.seal only act on
object properties (extended to private properties?).
But the broader problem they are addressing is reducing the mutability
of objects. WeakMaps, maps and sets bring a new form of mutability which
cannot be implemented in the form of private properties (I think at
least). So the question is:

Should Object.preventExtensions be extended to reduce WeakMaps, Maps and
Sets mutability? Likewise for Object.seal|freeze?

David


More information about the es-discuss mailing list