"Subclassing" basic types in DOM - best method?

Tab Atkins Jr. jackalmage at gmail.com
Tue Nov 20 12:20:32 PST 2012

On Tue, Nov 20, 2012 at 9:38 AM, Erik Arvidsson
<erik.arvidsson at gmail.com> wrote:
> On Tue, Nov 20, 2012 at 9:25 AM, Domenic Denicola
> <domenic at domenicdenicola.com> wrote:
>> For URLQuery in particular, since it's a String->String map, why not just
>> use a plain-old-JavaScript-object with appropriate interceptions via a
>> proxy? This provides a much more idiomatic API:
>> new URLQuery(object) stays the same
>> urlQuery.get(name) -> urlQuery[name][0]
>> urlQuery.getAll(name) -> urlQuery[name]
>> urlQuery.set(name, values) -> urlQuery[name] = values; use proxy here
>> urlQuery.add(name, values) -> urlQuery[name].push(...values); use proxy
>> here
>> urlQuery.has(name) -> Object.prototype.hasOwnProperty.call(urlQuery, name)
> That is not very nice.
>> urlQuery.delete(name) -> delete urlQuery[name]
>> urlQuery.delete(name, value) -> if (urlQuery[name]) { urlQuery[name] =
>> urlQuery[name].filter(x => x !== value); }
> I think the main issue with using an API like this is that you will end up
> with conflicts between the storage space and the API space. In your proposal
> there are no API methods but as soon as you add one you are limiting the
> keys you can use. It seems very likely that you don't want this to throw
> when people try to concat this with a string, so you add a toString method.
> Now, what happens when you do urlQuery['toString'] or worse
> urlQuery['__proto__'] = 'huh'?
> This kind of problem is hurting people all the time when they use
> localstorage

Yes, a real object (or a Proxy exposing an object-like API) is likely
hostile here.  It's okay in very limited instances, but it prevents
you from extending the API beyond CRUD, requiring ugly workarounds
like what you have for .has() and the 2-arg .delete().

Plus, as URLQuery is actually a MultiMap, not a plain Map (but is
friendly to being treated as a plain Map), we already know we want to
exceed that basic API, with things like the 2-arg delete().

On that note, are there any active proposals for MultiMaps in tc39?
It would be great to align URLQuery's API with tc39's plans, if they
have any.


More information about the es-discuss mailing list