javascript - ife 中 js递归练习题求解答
伊谢尔伦
伊谢尔伦 2017-04-11 13:04:23
[JavaScript讨论组]

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-...

小弟刚刚入门,希望大神给予指点一二,感激不尽

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(1)
伊谢尔伦

1.为了告诉你在这里又要扩展了,root下面又有东西啦
2.回调函数

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号