0
我創建了JSON對象,在這個示例中發生了循環引用錯誤。 爲什麼發生循環引用錯誤以及如何解決循環引用錯誤? 給出關於這個問題的全部細節? 代碼:爲什麼循環引用錯誤出現在下面的代碼中?以及如何解決它?
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>
<body>
<script type="text/javascript">
var data = { "Id": 100, "Name": "Name 1", "Children": [{ "Id": 200, "Name": "Name 2", "Children": null }, { "Id": 300, "Name": "Name 3", "Children": null }] };
var record=createRecord(data,null);
JSON.stringify(record);
function createRecord(data,parentItem) {
var record,
childDataSource = data["Children"];
//CLONE THE DATA OBJECT
record = $.extend({}, data);
record.parentItem = parentItem;
record.item = data;
record.childRecords = childDataSource && createChildRecords(childDataSource, record);
return record;
}
function createChildRecords(childDataSource, parentItem) {
var proxy = this,
records = [],
count = 0,
length = childDataSource.length,
record = null,
childRecord;
for (count = 0; count < length; count++) {
record = childDataSource[count];
if (record) {
childRecord = createRecord(record, parentItem);
records.push(childRecord);
}
}
return records;
}
</script>
</body>
</html>
我誤讀了電話 - 這是真正的原因 - 我的錯誤。偉大的眼睛,損壞! :) –