×

Welcome to TagMyCode

Please login or create account to add a snippet.
1
0
 
0
Language: Javascript
Posted by: Jing Tai Piao
Added: May 20, 2013 2:11 AM
Views: 928
Tags: no tags
  1. // Demo: Circular reference
  2. var o = {};
  3. o.o = o;
  4.  
  5. var cache = [];
  6. JSON.stringify(o, function(key, value) {
  7.     if (typeof value === 'object' && value !== null) {
  8.         if (cache.indexOf(value) !== -1) {
  9.             // Circular reference found, discard key
  10.             return;
  11.         }
  12.         // Store value in our collection
  13.         cache.push(value);
  14.     }
  15.     return value;
  16. });
  17. cache = null; // Enable garbage collection