# 如何获取页面中所有用到的 html 标签

//获取页面总标签 dom
const html$dom = document.querySelector("html");
//获取第一层子标签
const nodes = html$dom.childNodes;
//创建存放标签容器
const arr = [];
//遍历所有子标签里面的子标签...
const getChildNodes = data => {
    for (let index = 0; index < data.length; index++) {
        if (
            !arr.includes(data[index].nodeName.toLocaleLowerCase()) &&
            data[index].nodeName.indexOf("#") === -1
        ) {
            arr.push(data[index].nodeName.toLocaleLowerCase());
        }

        if (data[index].childNodes.length) {
            getChildNodes(data[index].childNodes);
        }
    }
};

getChildNodes(nodes);

# 实现一个快速排序的方法

const arr1 = [
    1.5,
    1,
    44,
    55,
    566,
    0.2,
    44544,
    0.0001,
    35556,
    0.466,
    78,
    2354,
    13,
    0,
    0.2,
    -0.3,
    -0.01,
    46,
    1,
    0.1,
    2
];

const sortFuc = function(arr) {
    if (arr.length <= 1) {
        //如果数组长度小于等于1无需判断直接返回即可
        return arr;
    }
    const pivotIndex = Math.floor(arr.length / 2); //取基准点
    const pivot = arr.splice(pivotIndex, 1)[0]; //取基准点的值,splice(index,1)函数可以返回数组中被删除的那个数
    // console.log(arr, arr[pivotIndex], pivot)
    const left = []; //存放比基准点小的数组
    const right = []; //存放比基准点大的数组
    for (let i = 0; i < arr.length; i++) {
        //遍历数组,进行判断分配
        if (arr[i] < pivot) {
            left.push(arr[i]); //比基准点小的放在左边数组
        } else {
            right.push(arr[i]); //比基准点大的放在右边数组
        }
    }
    return sortFuc(left).concat([pivot], sortFuc(right));
};

# 实现一个数组乱序方法

const arr2 = [1, 2, 3, 4, 5];

const mathFuc = (arr, i) => {
    for (let index = 0; index < arr.length; index++) {
        const rIndex = Math.floor(Math.random() * (index + 1));
        const temp = arr[rIndex];
        arr[rIndex] = arr[index];
        arr[index] = temp;
    }
    return arr;
};

mathFuc(arr2);

# 实现一个 LazyMan

# 要求

  • LazyMan(“Hank”)

  • 输出:

  • Hi! This is Hank!

  • LazyMan(“Hank”).sleep(10).eat(“dinner”)

  • 输出:

  • Hi! This is Hank!

  • 等待 10 秒..

  • Wake up after 10

  • Eat dinner~

  • LazyMan(“Hank”).eat(“dinner”).eat(“supper”)

  • 输出:

  • Hi This is Hank!

  • Eat dinner~

  • Eat supper~

  • LazyMan(“Hank”).sleepFirst(5).eat(“supper”)

  • 输出:

  • 等待 5 秒

  • Wake up after 5

  • Hi This is Hank!

  • Eat supper

//创建一个类
class _LazyMan {
    constructor(name) {
        this.tasks = [];
        const fn = () => {
            console.log(`Hi! This is ${name}!`);
            this.next();
        };
        this.tasks.push(fn);
        setTimeout(() => {
            this.next();
        }, 0); // 在下一个事件循环启动任务
    }

    // 事件调度函数
    next() {
        const fn = this.tasks.shift();
        fn && fn();
    }

    sleep(time) {
        const fn = () => {
            setTimeout(() => {
                console.log(`Wake up after ${time}s!`);
                this.next();
            }, time * 1000);
        };

        this.tasks.push(fn);
        return this; // 实现链式调用
    }

    sleepFirst(time) {
        const fn = () => {
            setTimeout(() => {
                console.log(`Wake up after ${time}s!`);
                this.next();
            }, time * 1000);
        };
        //放在第一个
        this.tasks.unshift(fn);
        return this; // 实现链式调用
    }

    eat(name) {
        const fn = () => {
            console.log(`Eat ${name}~`);
            this.next();
        };
        this.tasks.push(fn);
        return this; // 实现链式调用
    }
}
//创建调用方法
function LazyMan(name) {
    return new _LazyMan(name);
}

LazyMan("Hank");

LazyMan("Hank")
    .sleep(10)
    .eat("dinner");

LazyMan("Hank")
    .eat("dinner")
    .eat("supper");