JSON.stringify与自定义替换器一起使用。例如:
// Demo: Circular referencevar circ = {};circ.circ = circ;// Note: cache should not be re-used by repeated calls to JSON.stringify.var cache = [];JSON.stringify(circ, function(key, value) { if (typeof value === 'object' && value !== null) { if (cache.indexOf(value) !== -1) { // Duplicate reference found, discard key return; } // Store value in our collection cache.push(value); } return value;});cache = null; // Enable garbage collection在此示例中,替换器不是100%正确的(取决于您对“重复”的定义)。在以下情况下,将丢弃一个值:
var a = {b:1}var o = {};o.one = a;o.two = a;// one and two point to the same object, but two is discarded:JSON.stringify(o, ...);但是概念仍然存在:使用自定义替换器,并跟踪已解析的对象值。



