【问题标题】:Javascript returns undefined while logs objectJavascript在日志对象时返回未定义
【发布时间】:2016-12-05 23:19:40
【问题描述】:
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);
            }
        }
    }

【问题讨论】:

    标签: javascript oop nodes


    【解决方案1】:

    编辑:

    正如我们在 cmets 中讨论的那样,并且您向我提供了更多信息,我想出了以下几点:

    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 来解决此问题。
    所以this.childNodes[i].selectChildByAttrbuteValue(attribute, value); 都应该是return this.childNodes[i].selectChildByAttrbuteValue(attribute, value);

    此外,没有默认值,所以如果您正在寻找一个值,但没有一个属性包含该值,您也会得到undefined

    小提示:您当前允许在 if 语句中接受值 null,这可能是不受欢迎的行为。

    【讨论】:

    • @CreasoiDev 它看起来也不起作用。或者我做错了/。我发布了完整的课程来展示我在做什么
    • 快速提问,你打电话给selectChildByAttributeValue 还是selectChildByAttrbuteValueAttribute vs Attrbute)?
    • getElementsByAttributeValue(attribute, value) { var element = this.Document.selectChildByAttributeValue(attribute, value); return element; }
    • 你能举一个Node对象的例子吗?只需复制粘贴 JSON.stringify(nodeObject, null, 4); 的输出
    • 我得到或反对的那个?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 2021-07-07
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多