分类 JavaScript 下的文章

<html>
<body>
    <script type="text/javascript">
        const list = [
            { id: 1, name: '超级管理', parent_id: 0 },
            { id: 2, name: '用户管理', parent_id: 1 },
            { id: 3, name: '部门管理', parent_id: 1 },
            { id: 4, name: '日志管理', parent_id: 1 },
            { id: 5, name: '操作用户', parent_id: 2 },
            { id: 6, name: '查看用户', parent_id: 2 },
            { id: 7, name: '用户新增', parent_id: 5 },
            { id: 8, name: '用户删除', parent_id: 5 },
            { id: 9, name: '用户修改', parent_id: 5 },
            { id: 10, name: '操控部门', parent_id: 3 },
            { id: 11, name: '查看部门', parent_id: 3 },
            { id: 12, name: '部门新增', parent_id: 10 },
            { id: 13, name: '部门删除', parent_id: 10 },
            { id: 14, name: '部门修改', parent_id: 10 },
            { id: 15, name: '日志查看', parent_id: 4 },
            { id: 16, name: '日志导出', parent_id: 4 }
        ];
        /**
         * 树状的算法
         * @params list     代转化数组
         * @params parentId 起始节点
         */
        function getTrees(list, parentId) {
            let items = {};
            // 获取每个节点的直属子节点,*记住是直属,不是所有子节点
            for (let i = 0; i < list.length; i++) {
                let key = list[i].parent_id;
                if (items[key]) {
                    items[key].push(list[i]);
                } else {
                    items[key] = [];
                    items[key].push(list[i]);
                }
            }
            return formatTree(items, parentId);
        }

        /**
         * 利用递归格式化每个节点
         */
        function formatTree(items, parentId) {
            let result = [];
            if (!items[parentId]) {
                return result;
            }
            for (let t of items[parentId]) {
                t.children = formatTree(items, t.id)
                result.push(t);
            }
            return result;
        }


        var resultall = getTrees(list, 1);


        console.dir(resultall);
        console.log(JSON.stringify(resultall))
    </script>

</body>

</html>

return new Promise方式无限调用then:

new Promise((resolve, reject) => {
    console.log(1)
    resolve({
        name: "第1个传递的值"
    });
}).then((result) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log(2, result)
            resolve({
                name: "第2个传递的值"
            })
        }, 1000)
    })
}).then((result) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log(3, result)
            resolve({
                name: "第3个传递的值"
            })
        }, 1000)
    })
}).then((result) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log(4, result)
            resolve({
                name: "第4个传递的值"
            })
        }, 1000)
    })
}).then((result) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log(5, result)
        }, 1000)
    })
})

结果:
22222222222222222222.png
promise.all方法来实现

  var p1 = new Promise((resolve, reject) => {
      console.log(1)
      resolve()
    })
    var p2 = new Promise((resolve, reject) => {
      setTimeout(() => {
        console.log(2)
        resolve()
      }, 1000);
    })
    var p3 = new Promise((resolve, reject) => {
      setTimeout(() => {
        console.log(3)
        resolve()
      }, 1000);
    })
 
    Promise.all([p1, p2, p3]).then((result) => {
      console.log("执行完毕");
    })

**this 作用范围**
function Foo(){
   getName = function(){
     console.log(1);
   };
   return this;
}
Foo.getName = function(){
     console.log(2);
};
Foo.prototype.getName = function(){
     console.log(3);
};
var getName = function(){
     console.log(4);
};
function getName(){
     console.log(5);
}

Foo.getName();//2
getName();//4
Foo().getName();//1
getName();//1
new Foo.getName();//2
new Foo().getName();//3
new new Foo().getName();//3

全局的name,用以证明this指向的不是global

    var name = "global name";

    function showName() {
        this.name = "showName function";
    }
    var obj = new showName();

    console.log(obj.name); //showName function
    console.log(name); //global name