0

所以,我有这个功能。

function makeContent(jsonData) {
    var aProperty,
    containerType,
    contentContainerName,
    containerIdentifier,
    containerComment,
    theContent;
    console.log("jsonData = ");
    console.log(jsonData);
    var madeContent;

    for (aProperty in jsonData) {
        console.log("Working on property " + aProperty);
        console.log("With value of ");
        console.log(jsonData[aProperty]);
        switch (aProperty) {
        case "containerType": {
                containerType = jsonData[aProperty];
                break;
            }
        case "contentContainerName": {
                contentContainerName = jsonData[aProperty];
                break;
            }
        case "containerComment": {
                containerComment = jsonData[aProperty];
                break;
            }
        case "containerIdentifier": {
                containerIdentifier = jsonData[aProperty];
                break;
            }
        case "itemContent": {
                theContent = jsonData[aProperty];
                break;
            }
        }
    }

    if (typeof theContent !== 'undefined') {
        console.log("theContent =");
        console.log(theContent);
        if (theContent.hasOwnProperty) {
            if (madeContent != 'undefined') {
                madeContent = makeContent(theContent);
                madeContent = "<" + containerType + " " + containerIdentifier + "=\"" + contentContainerName + "\">" + madeContent + "</" + containerType + ">" + containerComment;
            }
        } else {
            madeContent = "<" + containerType + " " + containerIdentifier + "=\"" + contentContainerName + "\">" + theContent + "</" + containerType + ">" + containerComment
                console.log(madeContent);
            console.log("Else statement");
        }

    }

    return madeContent;
}

我的麻烦直到递归调用之后才开始。由于某种原因,在我以递归方式再次调用 makeContent() 之后,在 for 循环中遍历对象中的属性,我得到 aProperty 的 0。

JSON数据:

    {
        "contentContainerName" : "footer-bgcontent", 
        "containerType" : "div",
        "containerIdentifier" : "id",
        "containerComment" : "<!-- End #footer-bgcontent-->",
        "itemContent" : [ 
            {
                "contentContainerName" : "footer",
                "containerType" : "div",
                "containerIdentifier" : "id",
                "contentComment" : "<!-- End #footer -->", 
                "itemContent" : [
                    {
                        "contentContainerName" : "footerLink",
                        "containerType" : "a",
                        "containerIdentifier" : "id",
                        "contentTag" : "<a href=\"missionStatement.html\" id=\"footerLink\" title=\"Link to mission statement\"></a>"                   
                    },
                    {
                        "contentContainerName" : "footerContent",
                        "containerType" : "div",
                        "containerIdentifier" : "id",
                        "contentTag" : "<div id=\"footerContent\"></div><!-- End #footerContent-->",
                        "itemContent" : [
                            {
                                "contentContainerName" : "createdBy",
                                "containerType" : "p",
                                "containerIdentifier" : "id",
                                "contentTag" : "<p id=\"createdBy\"></p>",
                                "itemContent" : "Created by: Patrick McAvoy"
                            },
                            {
                                "contentContainerName" : "lastModified",
                                "containerType" : "p",
                                "containerIdentifier" : "id",
                                "contentTag" : "<p id=\"lastModified\"></p>",
                                "itemContent" : "Last Modified: "
                            },
                            {
                                "contentContainerName" : "copyright",
                                "containerType" : "p",
                                "containerIdentifier" : "id",
                                "contentTag" : "<p id=\"copright\"></p>",
                                "itemContent" : "Copright"
                            }
                        ]
                    }
                ]   
            }
        ]

    }

然后输出

    jsonData = 
footer.js:51
Object
footer.js:55Working on property contentContainerName
footer.js:56With value of 
footer.js:57footer-bgcontent
footer.js:55Working on property containerType
footer.js:56With value of 
footer.js:57div
footer.js:55Working on property containerIdentifier
footer.js:56With value of 
footer.js:57id
footer.js:55Working on property containerComment
footer.js:56With value of 
footer.js:57<!-- End #footer-bgcontent-->
footer.js:55Working on property itemContent
footer.js:56With value of 
footer.js:57[
Object
]
footer.js:83theContent =
footer.js:84[
Object
]
footer.js:50jsonData = 
footer.js:51[
Object
]
footer.js:55Working on property 0
footer.js:56With value of 
footer.js:57
Object
footer.js:38Made content: 
footer.js:39<div id="footer-bgcontent">undefined</div><!-- End #footer-bgcontent-->
4

1 回答 1

1

我不确定这是否会导致您的问题,但这条线是错误的:

if (theContent.hasOwnProperty) 

.hasOwnProperty是 JS 中每个对象/类型的方法,所以上面的测试总是正确的,所以你的代码总是递归的,即使.itemContent是一个字符串。

无论如何,代码都是不必要的复杂——无需遍历所有属性并测试每个键——只需将它们直接分配给所需的变量!

我相信下面的代码复制了你正在尝试做的事情并且短!

function makeContent(data) {
    var type = data.containerType || 'div',
        name = data.contentContainerName || '',
        id = data.containerIdentifier || 'id',
        comment = data.containerComment || '',
        content = data.itemContent || '';

    if (Array.isArray(content)) {
        content = content.map(makeContent).join('');
    }

    return '<' + type + ' ' + id + '="' + name + '">' + content +
           '</' + type + '>' + comment;
}

如果未指定,则|| 'foo'确保为字符串分配默认值。

请参阅http://jsfiddle.net/alnitak/rmTTg/contentTag - 注意:此代码对您自己的代码中也未使用的 proeprty 没有任何作用。

于 2013-05-01T20:58:59.603 回答