递归与非递归遍历

var properties = [];
function getProps(obj) {
    if (typeof target == 'object') {
        for (var key in obj) {
            if (obj.hasOwnProperty(key)) {
                properties.push(key);
                eachProp(obj[key]);
            }
        }
    }
}

function getProps2(obj) {
    var props = [];
    var queue = [obj];
    var target;

    while(target = queue.shift()) {
        if (target && target.constructor === Object) {
            for (var key in target) {
                props.push(key);
                queue.push(target[key]);
            }
        }
    }

    return props;
}

 

posted @ 2017-06-28 15:16  风之约  阅读(172)  评论(0编辑  收藏  举报