//程序运行完成时一定要有输出语句,本工具才能正确展示运行结果。 
console.log('1');

setTimeout(function(){
    console.log('2');
    process.nextTick(function(){
        console.log('3');
    })
    new Promise(function(resolve){
        console.log('4');
        resolve();
    }).then(function(){
        console.log('5');
    })
})

process.nextTick(function(){
    console.log('6');
})
new Promise(function(resolve){
    console.log('7');
    resolve();
}).then(function(){
    console.log('8');
})

setTimeout(function(){
    console.log('9');
    process.nextTick(function(){
        console.log('10');
    })
    new Promise(function(resolve){
        console.log('11');
        resolve();
    }).then(function(){
        console.log('12');
    })

})
toryang (Toryang )- 2019-08-26 0 人
事件循环机制(2)
async function async1(){
    console.log(1);
    await async2();
    console.log(2);
}
async function async2(){
    console.log(3);
}
console.log(4);

setTimeout(function(){
    console.log(5);
},0);

async1();

new Promise(function(resolve){
    console.log(6);
    resolve();
}).then(function(){
    console.log(7);
});
console.log(8);
toryang (Toryang )- 2019-08-26 0 人
事件循环机制(1)
function _instanceof(l, r) {
    l = l.__proto__;
    var O = r.prototype;

    while (true) {
        if (l == null) {
            return false;
        }

        if (l === O) {
            return true;
        }

        l = l.__proto__;
    }
}
toryang (Toryang )- 2019-07-10 0 人
实现一个 instanceof
function throttle(func, wait) {
    let previous = 0;
    return function () {
        let now = Date.now();
        let context = this;
        let args = [].slice.call(arguments);
        if (now - previous > wait) {
            func.apply(context, args);
            previous = now;
        }
    }
}

function throttle_1(func, wait) {
    let timeout;
    return function () {
        let context = this;
        let args = [].slice.call(arguments);
        if (!timeout) {
            timeout = setTimeout(() => {
                timeout = null;
                fn.apply(context, args);
            }, wait);
        }

    }
}
toryang (Toryang )- 2019-07-10 0 人
节流函数(时间戳版和定时器版)
// 非立即执行
function debounce(func, wait) {
    let timeout;
    return function () {
        let context = this;
        let args = [].slice.call(arguments);
        if (timeout) {
            clearTimeout(timeout)
        }
        timeout = setTimeout(()=>{
            func.apply(context,args);
        },wait)
    }
}
// 立即执行
function debounce_1(func,wait){
    let timeout
    return {
        function(){
            let args = [].slice.call(arguments);
            if(timeout) clearTimeout(timeout);
            let callNow = !timeout;
            timeout = setTimeout(()=>{
                timeout = null;
            },wait) 
            if(callNow) fn.apply(this,args);
        }
    }
}
toryang (Toryang )- 2019-07-10 0 人
防抖函数(立即执行和非立即执行)
function Subject(){
    let observers = [];
}

Subject.prototype.addObserver = function(ob){
    this.observers.push(ob)
}

Subject.prototype.notify = function(){
    for(let ob of this.observers){
        if(typeof ob == "function"){
            ob.call();
        }
    }
}
toryang (Toryang )- 2019-07-10 0 人
观察者模式
没有了
1/4 下一页