unstrungify
Walk a hierarchical object as JSON.stringify does but without serializing.
Synopsis
unstrungify
where:
object
is the context (thethis
value), which is the object to be walkedpreserve
is an optional boolean (defaults tofalse
)
Usage
var unstrungify = ; { /* ... */ };myAPIprototypegetState = unstrungify;var instance = ;var state = instance;
or without an API:
var unstrungify = ;var myObject = /* ... */ ;var state = unstrungify;
Description
Recursively walks the object tree.
For each node:
- calls
.toJSON()
when available (see below) - Otherwise clones the node
Returns a new object representing the context object.
Functionally identical to JSON.parse(JSON.stringify(this))
but far more efficient because it skips the serialization and parse steps.
toJSON
?
What the heck is this Learn more about JSON.stringify()
's toJSON behavior.
Undefined array elements
When precise index position in arrays is important, call with truthy preserve
parameter.
Cross-frame scripting caveat!
This function comes with its own unstrungify.isArray()
which is very fast but does not work for cross-frame scripting. In that case you can use the following (much slower) function:
unstrungifyisArray = unstrungifycrossFramesIsArray;
Or, if you have jQuery loaded, you might use it's (reasonably fast) duck typing function:
unstrungifyisArray = jQueryisArray;
If you are not doing any cross-frame scripting and speed is not an issue, you don't need this.