Deep cloning objects defined by JSON.

Rick Waldron waldron.rick at gmail.com
Sun Jan 22 12:29:21 PST 2012


>
> Potential issues
>
>  - subset of JSON is too restricted to be useful
>

This alone seems like a deal-breaker/non-starter. How would you copy
methods? Forgetting about cyclic reference exceptions for a moment:


var o = {
  s: "string",
  n: 1,
  a: [ 1, 2, 3, 4 ],
  o: {
    method: function( prop ) {
      return "stuff";
    },
    n: null,
    u: undefined
  },
  bt: true,
  bf: false
},
clone = JSON.parse(JSON.stringify(o));

> clone

{
  s: 'string',
  n: 1,
  a: [ 1, 2, 3, 4 ],
  o: {
    n: null
  },
  bt: true,
  bf: false
}


While it has the benefit of losing all of its references to the original
object, it also lost any methods or initialized but unassigned (undefined)
properties. Security concern trumps the inclusion of methods in valid JSON

Rick





>  - Proxies/private state may cause issues (the same issues would apply to
> JSON.stringify ?)
>  - What's the value of the [[Prototype]] of the clone? (JSON.parse uses
> the standard [[Prototype]] for the stringified object)
>  - Do we expect anything sensible to happen with host objects? (JSON.parse
> returns objects with few or no properties for host objects)
>  - Do we solve cyclic references? (JSON.parse fails on them)
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120122/91e5272f/attachment.html>


More information about the es-discuss mailing list