Trying to convert the following function to Coffeescript:
var parse = function开发者_如何转开发 (elem) {
for each(var subelem in elem) {
if (subelem.name() !== null ) {
console.log(subelem.name());
if (subelem.children().length() > 0) {
parse(subelem);
}
} else {
console.log(subelem);
}
}
};
var xml = new XML(content);
parse(xml);
It merely prints the element tags and any text to the console.
Tried using:
parse = (elem) ->
if elem.name()?
console.log elem.name()
if elem.children().length() > 0
parse subelem for own elkey, subelem of elem
else
console.log elem
xml = new XML content
parse subelem for own elkey, subelem of xml
But it never seems to parse anything under the root xml node and ends up in an infinite recursion loop continuously printing out the root nodes tag until it blows up. Any ideas as to what I am doing wrong? Thanks.
Hmm. I tested this, and the issue seems to go away if you drop the own
keyword, which adds a hasOwnProperty
check. Somehow, the first child of each element seems to pass that check, while others fail it. I'm a bit mystified by this, but there's your answer.
精彩评论