Overriding Map/etc with get/set hooks?

Tab Atkins Jr. jackalmage at gmail.com
Tue May 21 23:44:09 PDT 2013


On Tue, May 21, 2013 at 9:20 PM, Ron Buckton <rbuckton at chronicles.org> wrote:
> What if the default Map prototype had a configurable but non-writable data
> property for a @@coerceKey symbol that pointed to a default coercion
> function. You could subclass Map and provide your own @@coerceKey
> implementation. Then Map.prototype.set.call() would be forced to run the
> custom coercion function.
>
> A @@coerceKey override could be used to coerce keys from one type to
> another, or to provide key validation if you want keys in a specific format.
> By providing a default implementation that is basically an identity
> function, you maintain the same current expectations for Map. This then
> gives developers the ability to further customize the behavior of Map in
> subclasses, giving the class more flexibility.
>
> If that were the case, there could also be a @@coerceValue symbol on
> Map.prototype as well as Set.prototype. Possibly even Array.prototype as
> well for Array subclasses, as a means of limiting array contents to a
> specific type. The coercion functions could also be used to allow a subclass
> to Mark itself as read-only and throw on attempts at modification. Default
> implementations could verify whether the coercion function has changed from
> the default and skip the coercion calls as a performance optimization.

These aren't bad ideas, and they get me most of the way to where I
need to be.  I still need some way to detect when an entry is
added/modified/deleted, so I can update the style rule it's associated
with.

~TJ


More information about the es-discuss mailing list