On Wed, Oct 3, 2012 at 2:12 PM, Axel Rauschmayer <a...@rauschma.de> wrote:

> I always find it next to impossible to guess use cases for non-trivial
> features. So having those would help. Often YAGNI applies. Two
> possibilities:
> - Subtype Map and override toJSON
> - Use JSON.stringify() with a replacer
>
> As Brandon Benvie mention, we could standardize on an array of pairs, but
> my (not very educated) guess would be that converting to objects makes more
> sense.
>
>

Map.prototype.toJSON = function() {
  // somehow iterate keys and values into an array pair?
  // for now, I'll fake it.
  return '[ [ { "foo": "whatever" }, [ 1, 2, 3, 4, 5 ] ], [ {}, "my key is
a plain object" ] ]';
};

function Key(foo) {
  this.foo = foo;
}

var map = new Map(),
    key = new Key("whatever"),
    obj = {};


map.set(key, [ 1, 2, 3, 4, 5 ]);
map.set(obj, "my key is a plain object");

console.log( map.get(key) ); // [ 1, 2, 3, 4, 5 ]
console.log( map.get(obj) ); // "my key is a plain object"

console.log( map.toJSON() );

// [ [ { "foo": "whatever" }, [ 1, 2, 3, 4, 5 ] ], [ {}, "my key is a plain
object" ] ]

Now imagine this map is stored in some kind of NoSQL
document store and will be later pulled out by some
other functionality in our application.

How are the map keys revived _into_scope_?

console.log( JSON.parse(map.toJSON()) );

This just makes an array of pairs.


Rick







> On Oct 3, 2012, at 19:57 , Rick Waldron <waldron.r...@gmail.com> wrote:
>
>
>
> On Wed, Oct 3, 2012 at 1:35 PM, Axel Rauschmayer <a...@rauschma.de> wrote:
>
>> This also made me wonder about Maps, if the same use case were applied -
>> toJSON simply wouldn't work when you have an object as a key.
>>
>>
>> The default should probably be to convert to an object who’s keys are the
>> results of applying String() to the map’s keys. Additionally, one could
>> introduce a method toPairArray() that converts a Map into an array of pairs
>> (2-element arrays) – that can be JSON-ified. The alternatives are:
>>
>> 1. Switch to pairs if the keys are not strings.
>> 2. Allow a map to be configured which of the two representations should
>> be used for toJSON.
>> 3. Give toJSON a parameter whose default is to produce an object.
>>
>> Given JSON.toStringify(), I’m not sure that #2 is necessary and that #3
>> is useful. #1 might work, but seems like a big change in representation and
>> it takes time to check all the keys.
>>
>>
> My concern was actually about the references (for keys) that would be
> broken, how would you get those back? There is no reasonably sane way that
> doesn't involve magic scope memory tables and polluters.
>
> Unless I'm looking at it the wrong way...
>
> Rick
>
>
>
>
>
>
>>         --
>> Dr. Axel Rauschmayer
>> a...@rauschma.de
>>
>> home: rauschma.de
>> twitter: twitter.com/rauschma
>> blog: 2ality.com
>>
>>
>
>         --
> Dr. Axel Rauschmayer
> a...@rauschma.de
>
> home: rauschma.de
> twitter: twitter.com/rauschma
> blog: 2ality.com
>
>
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to