编辑代码

//JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 

function eachBefore(callback,that){
    var node = this,
        nodes = [node],
        index = -1,
        i,children;
    
    while(node = nodes.pop()){
        callback.call(that,node,++index,this)
        if(children = node.children){
            for(i = children.length - 1;i >=0;--i){
                nodes.push(children[i])
            }
        }
    }
}


var aa = {
    name:'a',
    children:[
        {
            name:'b',
            children:[
                {
                    name:'d'
                },
                {
                    name:'f'
                }
            ]
        },
        {
            name:'c',
            children:[
                {
                    name:'e'
                }
            ]
        }
    ]
}

aa.eachBefore = eachBefore

aa.eachBefore(function(node){
    console.log(node.name)
})