【发布时间】: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