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-...
小弟刚刚入门,希望大神给予指点一二,感激不尽
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
1.为了告诉你在这里又要扩展了,root下面又有东西啦
2.回调函数