javascript - ife 中 js递归练习题求解答
本文介绍了javascript - ife 中 js递归练习题求解答的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
1.this._root
括号中引入这个是什么意思
2.
tree.traverseDF(function(node){
console.log(node.data)
});
传入node,搞不懂,这一段都没明白啥意思
3.上源码
function Node(data) {
this.data = data;
this.parent = null;
this.children= [];
}
function Tree(data){
var node = new Node(data);
this._root = node;
}
// var tree = new Tree('ceo');
// tree._root;
//使用DFS方式便利树
Tree.prototype.traverseDF = function(callback){
//递归方式便利
(function recurse(currentNode){
for(var i = 0, length = currentNode.children.length; i < length; i++){
recurse(currentNode.children[i])
}
callback(currentNode)
})(this._root)
};
var tree = new Tree('one');
tree._root.children.push(new Node('two'));
tree._root.children[0].parent = tree;
tree._root.children.push(new Node('three'));
tree._root.children[1].parent = tree;
tree._root.children.push(new Node('four'));
tree._root.children[2].parent = tree;
tree.traverseDF(function(node){
console.log(node.data)
});
console.log(tree.traverseDF(node))
看的文章原文地址:https://code.tutsplus.com/zh-...
小弟刚刚入门,希望大神给予指点一二,感激不尽
解决方案
1.为了告诉你在这里又要扩展了,root下面又有东西啦
2.回调函数
这篇关于javascript - ife 中 js递归练习题求解答的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文