• JS模拟实现题目(new debounce throwee 等)


    模拟new实现

    function newObject() {
        let obj = new Object();
        let Con = [].shift.apply(arguments)
        obj.__proto__ = Con.prototype;
        let res = Con.apply(obj,arguments)
        return typeof res == "object" ? res : obj;
    }
    

    模拟instanceOf

    function instanceOf(left,right) {
        let proto = left.__proto__;
        let prototype = right.prototype
        while(true) {
            if(proto == null) return false
            if(proto == prototype) return true
            proto = proto.__proto__;
        }
    }
    

    防抖 debounce

    function debounce(fn,wait=50,immediate) {
        let timer;
        return function() {
            if(immediate) {
                fn.apply(this,arguments)
            }
            if(timer) clearTimeout(timer)
            timer = setTimeout(()=> {
                fn.apply(this,arguments)
            },wait)
        }
    }
    

    节流 throttle

    function throttle(fn,wait=50) {
        let timer;
        return function() {
            if(!timer) {
                timer = setTimeout(()=> {
                    fn.apply(this,arguments)
                    timer = null
                },wait)
            }
        }
    }
    

    jsonp

    function jsonp(url,callback,success) {
        let script = document.createElement("script")
        script.src = url;
        script.async = true;
        script.type = "text/javascript"
        window[callback] = function(data) {
            success && success(data)
        }
        document.body.append(script)
    }
    

    继承

    function a() {
        this.a = "a"
    }
    a.prototype.test = function() {
        console.log(this.a)
    }
    
    function b() {
        a.call(this);
        this.b = "b"
    }
    
    function tmp() {}
    tmp.prototype = a.prototype;
    b.prototype = new tmp();
    b.prototype.construcotr = b;
    

    模拟call

    Function.prototype.mycall = function(content = window) {
        content.fn = this;
        let args = [...arguments].slice(1);
        let result = content.fn(...args);
        delect content.fn;
        return result;
    }
    

    模拟apply

    Function.prototype.myapply = function(content = window) {
        content.fn = this;
        let result;
        
        if(argument[1]) {
            result = content.fn(...argument[1]);
        } else {
            result = content.fn();
        }
        delect content.fn;
        return result;
    }
    

    模拟bind

    Function.prototype.mybind = function(content) {
        if(typeof this != "function") {
            throw Error("not a function")
        }
        let fn = this;
        let args = [...arguments].slice(1);
        
        let resFn = function() {
            return fn.apply(this.instance == resFn ? this : content,args.concat(...arguments) )
        }
        function tmp() {}
        tmp.prototype = this.prototype;
        resFn.prototype = new tmp();
        
        return resFn;
    }
  • 相关阅读:
    【bzoj3110】[Zjoi2013]K大数查询 权值线段树套区间线段树
    【bzoj3196】Tyvj 1730 二逼平衡树 线段树套Treap
    【bzoj1189】[HNOI2007]紧急疏散evacuate BFS最短路+动态加边网络流
    【bzoj3527】[Zjoi2014]力 FFT
    【bzoj4259/bzoj4503】残缺的字符串/两个串 FFT
    【bzoj4827】[Hnoi2017]礼物 FFT
    【bzoj2194】快速傅立叶之二 FFT
    【bzoj2179】FFT快速傅立叶 FFT
    【bzoj4327】JSOI2012 玄武密码 AC自动机
    【bzoj3238】[Ahoi2013]差异 后缀数组+单调栈
  • 原文地址:https://www.cnblogs.com/dobeco/p/11295287.html
Copyright © 2020-2023  润新知