簡體   English   中英

日志對象時Javascript返回未定義

[英]Javascript returns undefined while logs object

selectChildByAttrbuteValue(attribute, value)
    {
        if(this.childNodes.length!=0)
        {
            for(var i=0; i<this.childNodes.length; i++)
            {
                if(typeof this.childNodes[i].attributes[attribute]!="undefined")
                {
                    if(this.childNodes[i].attributes[attribute]==value)
                    {
                        return this.childNodes[i];

                    }else
                    {
                        this.childNodes[i].selectChildByAttrbuteValue(attribute, value);
                    }
                }else
                {
                    this.childNodes[i].selectChildByAttrbuteValue(attribute, value);
                }
            }
        }

    }

當我執行console.log(this.childNodes [i])時,此代碼返回未定義的代碼; 記錄我的對象。 那么我應該返回它..但是它返回未定義的!

全班

class Node
{
    constructor(nodeName, nodeType)
    {
        this.nodeName = nodeName;

        this.nodeType = nodeType;
        this.attributes = {};
        this.childNodes = [];
        this.parentNode = null;


    }

    removeChild(node)
    {
        if(node.parentNode!=null)
        {
            for(var i=0; i<this.childNodes.length; i++)
            {
                if(node == this.childNodes[i])
                {
                    this.childNodes.splice(i, 1);
                    node.parentNode = null;
                }
            }
        }
    }

    appendChild(child)
    {
        if(child.parentNode==null)
        {
            this.childNodes.push(child);
            child.parentNode = this;

        }else
        {
            child.parentNode.removeChild(child);
            this.childNodes.push(child);
            child.parentNode = this;

        }
    }

    selectChildByAttrbuteValue(attribute, value)
    {

        if(this.childNodes.length!=0)
        {
            for(var i=0; i<this.childNodes.length; i++)
            {
                if(typeof this.childNodes[i].attributes[attribute]!="undefined")
                {
                    if(this.childNodes[i].attributes[attribute]==value)
                    {
                        return this.childNodes[i];

                    }else
                    {
                        this.childNodes[i].selectChildByAttrbuteValue(attribute, value);
                    }
                }else
                {
                    this.childNodes[i].selectChildByAttrbuteValue(attribute, value);
                }
            }
        }

    }

}

一堂完整的課,看看它到底如何循環。

Xml_Node {
  nodeName: 'row',
  nodeType: 'XML_ELEMENT',
  attributes: { name: 'Lee Pellion', characterID: '93746314' },
  childNodes: [],
  parentNode: 
   Xml_Node {
     nodeName: 'rowset',
     nodeType: 'XML_ELEMENT',
     attributes: 
      { name: 'characters',
        key: 'characterID',
        columns: 'name,characterID' },
     childNodes: [ [Circular] ],
     parentNode: 
      Xml_Node {
        nodeName: 'result',
        nodeType: 'XML_ELEMENT',
        attributes: {},
        childNodes: [Object],
        parentNode: [Object],
        innerText: '' },
     innerText: '' },
  innerText: '' }

根對象

Xml_Node {
  nodeName: 'root',
  nodeType: 'XML_ELEMENT',
  attributes: {},
  childNodes: 
   [ Xml_Node {
       nodeName: 'currentTime',
       nodeType: 'XML_ELEMENT',
       attributes: {},
       childNodes: [],
       parentNode: [Circular],
       innerText: '2016-12-06 01:20:09' },
     Xml_Node {
       nodeName: 'result',
       nodeType: 'XML_ELEMENT',
       attributes: {},
       childNodes: [Object],
       parentNode: [Circular],
       innerText: '' },
     Xml_Node {
       nodeName: 'cachedUntil',
       nodeType: 'XML_ELEMENT',
       attributes: {},
       childNodes: [],
       parentNode: [Circular],
       innerText: '2017-01-06 01:20:09' } ],
  parentNode: null,
  innerText: '' }

與控制台日志一起工作,但隨后對象消失在某個地方!

selectChildByAttributeValue(attribute, value) {

    if (this.childNodes.length != 0) {
        for (var i = 0; i < this.childNodes.length; i++) {

            if (typeof this.childNodes[i].attributes[attribute] != 'undefined') {
                if (this.childNodes[i].attributes[attribute] == value) {
                    console.log(this.childNodes[i]);
                    return this.childNodes[i];
                } else {
                    return this.childNodes[i].selectChildByAttributeValue(attribute, value);
                }
            } else {
                return this.childNodes[i].selectChildByAttributeValue(attribute, value);
            }
        }
    }

編輯:

正如我們在評論中提到的那樣,您已為我提供了更多信息,我提出了以下建議:

  1. 該函數應如下所示:

     function selectChildByAttributeValue(attribute, value) { if (this.childNodes.length != 0) { for (var i = 0; i < this.childNodes.length; i++) { if (typeof this.childNodes[i].attributes[attribute] != 'undefined') { if (this.childNodes[i].attributes[attribute] == value) { return this.childNodes[i]; } else { return this.childNodes[i].selectChildByAttributeValue(attribute, value); } } else { return this.childNodes[i].selectChildByAttributeValue(attribute, value); } } } } 

    使用return語句后,它可以正確返回嵌套的子級。

  2. 您的主節點沒有任何子節點,這就是為什么它返回undefined的原因。
    我在終端中執行了以下操作,並找到了正確的節點。

     const n = new Node('row', 'XML_ELEMENT'); n.appendChild(new Node('rowset', 'XML_ELEMENT')); n.appendChild(new Node('rowset', 'XML_ELEMENT')); n.childNodes[0].attributes = {name: 'characters', key: 'characterID', columnds: 'name, characterID'}; n.selectChildByAttributeValue('name', 'characters'); 

    結果如下:

     Node { nodeName: 'rowset', nodeType: 'XML_ELEMENT', attributes: { name: 'characters', key: 'characterID', columnds: 'name, characterID' }, childNodes: [], parentNode: Node { nodeName: 'row', nodeType: 'XML_ELEMENT', attributes: {}, childNodes: [ [Circular], [Object] ], parentNode: null } } 

我的根對象(在上面聲明為n )如下:

Node {
  nodeName: 'row',
  nodeType: 'XML_ELEMENT',
  attributes: {},
  childNodes: 
   [ Node {
       nodeName: 'rowset',
       nodeType: 'XML_ELEMENT',
       attributes: [Object],
       childNodes: [],
       parentNode: [Circular] },
     Node {
       nodeName: 'rowset',
       nodeType: 'XML_ELEMENT',
       attributes: {},
       childNodes: [],
       parentNode: [Circular] } ],
  parentNode: null }

舊答案

您的函數在自身中循環查找內容,但是只有1個return語句。
因此,當它調用自身以查看子值並返回某些內容時,它不會存儲在變量中或也不會返回,因此將其作廢。

您可以通過將return放在嵌套調用之前來解決此問題。
因此,這兩個this.childNodes[i].selectChildByAttrbuteValue(attribute, value); 應該return this.childNodes[i].selectChildByAttrbuteValue(attribute, value);

另外,沒有默認值,因此,如果您要查找一個值,但沒有一個屬性保存該值,那么您還將獲得undefined

小提示:您當前允許在if語句中接受null值,這可能是有害的行為。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM