Save Object.observe()! (please) + make WeakMap/WeakSet observable.

Coroutines coroutines at gmail.com
Mon Nov 2 22:34:28 UTC 2015


I was referred to es-discuss after filing a feature request here to
support observing WeakMaps and WeakSets.  (
https://bugzilla.mozilla.org/show_bug.cgi?id=1206584 )

I was linked this message:
https://mail.mozilla.org/pipermail/es-discuss/2015-November/044684.html

Please save Object.observe().

When I read that above posting it sounds like O.o() is to be removed
because it has not been popularly used.  It is proposed for ES7 - it's
not even part of an accepted standard yet.  I think it's strange to
remove something because it's not used much... because it's
experimental.  Who would depend on something non-standard?  Sure
there's a polyfill but it polls an object for changes with timers -
this sounds ridiculously inefficient.  If it doesn't get accepted
there will be no way to watch changes as they happen, instead of after
the fact.

I come from Lua.  In Lua we make proxy objects with metamethods.  You
create an empty table/object and define a metatable with a __index and
__newindex to catch accesses and changes when a key/property doesn't
exist.  I would primarily use this in sandboxes where I wanted to
track the exact series of operations a user was performing to modify
their environment (the one I'd stuck them in).  The important part to
remember is that I didn't know what properties of which objects they
would be accessing.  I couldn't just define a static list of getters
and setters and hope they make use of my limited, catching interface.
I think Object.observe() is very important for situations where you
want to watch all changes, because you don't know which properties
will be used.

I have a further proposal:  I wish it were possible to watch changes
on WeakMaps and WeakSets.

In Lua you can have weak references by setting the __mode = 'kv' (k
for key, v for value) in the object's metatable.  The only way I could
get weak references in JS was with WeakMaps and WeakSets.  Lua also
lets you set a __gc in the metatable to handle when a pair is
collected by the GC.  I wanted the ability to "see" when memoized
function calls are forgotten, but I can't do that with just
Object.observe() - as you can't observe a WeakSet or WeakMap.  I'd
love for this to be possible.

For debugging reasons it would be excellent to be able to watch when
things get collected (through weak references).  Finalizers in general
would be nice.  I mean in the sense that I want to do something
independent of the object being collected, not that I need to destroy
the object in a special way before collection.  I feel like there's a
vague difference between finalizer and destructor.

Please save Object.observe(), make WeakMap/WeakSet observable, and
save the whales too or something.

Toodles ~


More information about the es-discuss mailing list