面试遇到的手写函数

防抖函数
// 防抖函数
function debounce(fn, delay, immediate) {
   
    let timer = null;
    return function() {
   
        const _this = this;
        const args = [...argumets];
        if (immediate && !timer) {
   
            fn.apply(_this, args);
        }
        timer && clearTimeout(timer);
        timer = setTimeout(_ => fn.apply(_this, args), delay);
    }
}
截流函数
// 截流函数
function thtottle(fn, delay) {
   
    let start = Date.now();
    let timer = null;
    return funciton() {
   
        const _this = this;
        const args = [...argumets];
        let current = Date.now();
        let remain = delay - (current - start);
        timer && clearTimeout(timer);
        if (remain <= 0) {
   
            fn.apply(_this, args);
            start = Date.now();
        } else {
   
            setTimeout(_ => fn.apply(_this, args), remain)
        }
    }
}
对象深拷贝
// 对象深拷贝
function deepClone(obj) {
   
    let result;
    if (Array.isArray(obj)) {
   
        result = [];
    } else if (obj && typeof obj === 'object') {
   
        result = {
   };
    } else {
   
        return obj;
    }
    for (let key in obj) {
   
        if (obj.hasOwnProperty(key)) {
   
            result[key] = deepClone(obj[key])
        }
    }
    return result;
}
模拟promise
// 模拟promise
class MockPromise {
   
    constructor(fn) {
   
        this.state = pending;
        this.value = undefined;
        this.reason = undefined;
        const resolve = value => {
   
            if (this.state === 'pending') {
   
                this.state = 'fulfilled';
                this.value = value;
            }
        };
        const reject = value => {
   
            if (this.state === 'pending') {
   
                this.state = 'rejected'
                this.reason = value
            }
        };
        try {
   
            fn(resolve, reject);
        } catch (e) {
   
            reject(e);
        }
    }
    then(onResolve, onReject) {
   
        switch (this.state) {
   
            case 'fulfilled':
                onFulfilled();
                break;
            case 'rejected':
                onRejected();
                break;
            default:
        }
    }
}
模拟create
// 模拟create
function mockCreate(obj) {
   
    Function F() {
   };
    F.prototype = obj;
    return new F();
}
模拟call实现
// 模拟call实现
if (!Function.prototype.call) {
   
    Function.prototype.call = function(context) {
   
        if (typeof this !== 'function') {
   
            // throw new Error('must function');
        }
        context = context || window;
        const args = arguments.slice(1);
        context.fn = this;
        const result = context.fn(...args);
        delete context.fn;
        return result;
    }
}
模拟apply实现
// 模拟apply实现
if (!Function.prototype.apply) {
   
    Function.prototype.apply = function(context) {
   
        if (typeof this !== 'function') {
   
            // throw new Error('must function');
        }
        context = context || window;
        context.fn = this;
        const args = arguments.slice(1);
        const result = context.fn(...args);
        delete context.fn;
        return result;
    }
}
模拟bind实现
// 模拟bind实现
if(!Function.prototype.bind){
   
  Function.prototype.bind = function(context){
   
    // 首先判断this是不是function
    if(typeof this !== 'function'){
   
      // 抛出错误
    }
    var _this = this,
          fNOP = function(){
   },    // 用于后面维护原型关系
          args = Array.prototype.slice.call(arguments, 1),    // 取出bind收到的参数,除去第一个作为上下文的参数并保存
          fBound = function(){
   
            _this.call(context, args.concat(Array.prototype
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值