谁能告诉我为什么这将是一个循环引用?或至少让我指出什么是循环参考? [英] Can anyone tell me why this would be a circular reference? Or at least point me to what a circular reference is?

查看:118
本文介绍了谁能告诉我为什么这将是一个循环引用?或至少让我指出什么是循环参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码片段,该代码片段在其他地方也很好用,但是当我将其移到其他部分时却给了我一个循环引用错误.我什至找不到任何地方都可以使用的循环引用.

I have this code snippet, which works great somewhere else, but gives me a circular reference error when I move it to a different section. I can't even find a good reference on what a circular reference is anywhere.

// Create a new array to hold each of the Properties from the custom search pane
// This array will eventually be converted to JSON and become a List<Property>
propertyTables = [];

// Create new Object to hold a row - we have to construct these 
// Property objects manually for each row
propertyRow = {};

// This needs to be rewritten to include all of hidden input elements from the custom object that is clicked
// For each of the data elements the properties table, add it to the Object
$(this).parent().find('.editablePropertyList .customPropertyPrompt, .editablePropertyList .customPropertyDataType, .editablePropertyList .customPropertyInquirySearchType, .editablePropertyList .customPropertyID, .editablePropertyList .customPropertyText').each(function(index) {
    propertyValue = $(this).val();
    propertyText = $(this).text();
    switch ($(this).attr("class")) {
        case "customPropertyID":
            propertyRow.propertyID = propertyValue;
            break;
        case "customPropertyDataType":
            propertyRow.dataType = propertyValue;
            break;
        case "customPropertyPrompt":
            propertyRow.prompt = propertyText;
            break;
        case "customPropertyInquirySearchType":
            propertyRow.inquirySearchType = propertyValue;
            break;
        case "customPropertyText":
            // Whenever it reaches this data element, this means
            // that the iteration is at the end of a row.  Push the
            // newly filled propertyRow object (typeof Property) on
            // the PropertyTable array.  Then reinstance the propertyRow
            // object and it will start populating with the next row
            // as the next iteration occurs with propertyID
            propertyRow.inquirySearchText = propertyValue;
            if (propertyRow.inquirySearchText !== "") {
                propertyTables.push(propertyRow);
            }
                propertyRow = {};
                break;
            }
    });


    var statusFilter = [];
    var limitAnnotation = [];


    searchCriteria = {}; // Created the object
    searchCriteria.topFolderListBox = topFoldersWithSomeSelected; // Add the List<topLevelFolders> as the first property
    searchCriteria.docTypesListBox = docTypesWithSomeSelected; // Add the List<DocumentType> as the second property
    searchCriteria.propertyTable = propertyTables; // Add the List<Property> as the third property
    searchCriteria.statusFilter = statusFilter; // Add the List<statusFilter> as the fourth property
    searchCriteria.limitAnnotation = limitAnnotation; // Add the List<limitAnnotation> as the fifth property
    searchCriteria.fromDate = ""; // Add the string fromDate as the sixth property
    searchCriteria.toDate = ""; // Add the string toDate as the seventh property
    searchCriteria.dateRangeRelativeToday = false;
    searchCriteria.annotationText = ""; // Add the string annotationText as the eigth and final property

    // Convert to JSON String - craps out here with circular reference error
    textToSend = JSON.stringify(searchCriteria, null, "");

推荐答案

循环引用

简而言之:就是当objA包含对objB的引用,而该引用又包含对objA ..的引用,依此类推.您将拥有一个无限的序列.

In short: It is when objA contains a reference to objB which in turn contains a reference to objA.. and so on. You will have an infinite series like that.

最简单的例子:

var a = {}
var b = {}
a['x'] = b;
b['y'] = a;

在上述情况下,a对象包含引用b的键x.并且以类似的方式,b对象包含键y,该键再次引用a.

In the above case, a object contains a key x which refers to b. And in the similar fashion, b object contains a key y which refers to a back again.

这是序列化时的经典问题(在这种情况下类似于JSON):

This is a classic problem while serialising (like JSON in this case):

serialise a ->
    serialise value of key x in a ->  # == b
        serialise b ->
            serialise value of key y in b -> # ==a
                serialise a ->
                    ... and so on..

尝试上述代码时,我在Chrome中收到的错误:

The error that I get (in Chrome) when trying the above code:

TypeError: Converting circular structure to JSON


关于您的问题,在不查看整个代码的情况下,很难分辨出循环引用的确切位置.我建议一件事.执行console.log(searchCriteria).如果您的浏览器显示出类似树的结构,请继续扩展节点,直到您击中了以前见过的节点(深度较小).


About your problem, it is really difficult to tell where exactly is the circular reference without looking at the entire code. I would suggest one thing. Do a console.log(searchCriteria). If your browser shows a tree like structure go on expanding the nodes until you hit a node which you have seen before (at lesser depth).

当您看到类似下面的内容时,您知道罪魁祸首是什么. :)

And when you see something like the below, you know what is the culprit. :)

这篇关于谁能告诉我为什么这将是一个循环引用?或至少让我指出什么是循环参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆