Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is the code that I am using to detect circular references, it uses the technique that was suggested in the <a href="https://stackoverflow.com/a/7583161/592253">accepted answer by asbjornu</a>, whereby each value is walked through and its reference is maintained in an array so that the next value can be compared with those previously walked.</p> <pre><code>function isCircular(obj, arr) { "use strict"; var type = typeof obj, propName, //keys, thisVal, //iterKeys, iterArr, lastArr; if (type !== "object" &amp;&amp; type !== "function") { return false; } if (Object.prototype.toString.call(arr) !== '[object Array]') { //if (!Array.isArray(arr)) { type = typeof arr; // jslint sake if (!(type === "undefined" || arr === null)) { throw new TypeError("Expected attribute to be an array"); } arr = []; } arr.push(obj); lastArr = arr.length - 1; for (propName in obj) { //keys = Object.keys(obj); //propName = keys[iterKeys]; //for (iterKeys = keys.length - 1; iterKeys &gt;= 0; iterKeys -= 1) { thisVal = obj[propName]; //thisVal = obj[keys[iterKeys]]; type = typeof thisVal; if (type === "object" || type === "function") { for (iterArr = lastArr; iterArr &gt;= 0; iterArr -= 1) { if (thisVal === arr[iterArr]) { return true; } } // alternative to the above for loop /* if (arr.indexOf(obj[propName]) &gt;= 0) { return true; } */ if (isCircular(thisVal, arr)) { return true; } } } arr.pop(); return false; } </code></pre> <p>This code is available on <a href="http://jsfiddle.net/Xotic750/sgPWX/22/" rel="nofollow noreferrer">jsfiddle</a>, where you can test it for yourself. I have also run some performance tests on <a href="http://jsperf.com/iscircularobject-vs-iscircular-vs-iscirculares5/3" rel="nofollow noreferrer">jsperf</a>.</p> <p><code>Array.indexOf</code> was only introduced as of Javascript 1.6, see <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf" rel="nofollow noreferrer">MDN page</a></p> <p><code>Array.isArray</code> was only introduced as of Javascript 1.8.5, see <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/isArray" rel="nofollow noreferrer">MDN page</a></p> <p><code>Object.keys</code> was only introduced as of Javascript 1.8.5, see <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/keys" rel="nofollow noreferrer">MDN page</a></p> <p>It is also worth noting that <em><a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope/arguments/callee" rel="nofollow noreferrer"><code>arguments.callee</code></a></em> is deprecated and forbidden in strict mode in preference to using named functions</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload