javascript手写题

判断数据类型

function myTypeOf(obj) {
            let res = Object.prototype.toString.call(obj).split(' ')[1]
            res=res.substring(0, res.length - 1).toLowerCase()
            return res
        }

原型链继承

function animal(){
            this.color=['red','black']
}
function dog(){}
dog.prototype=new animal()

构造函数继承

function animal2(name){
            this.name='ad'
        }
        function dog2(name){
            animal2.call(this,name)
        }

寄生组合式继承

function tool(tar){
            function f(){}
            f.prototype=tar
            return f
        }
        function inherit(far,son){
            let prototype=tool(far)
            prototype.constructor=son
            son.prototype=prototype
        }

class实现继承

class far{
            constructor(name){
                this.name=name
            }
        }
        class son extends far{
            constructor(name){
                super(name)
            }
        }

数组扁平化

//es5
function flatten(arr){
            let res=[]
            for(let i=0,len=arr.length;i<len;i++){
                if(Array.isArray(arr[i])){
                    res=res.concat(flatten(arr[i]))
                }else{
                    res.push(arr[i])
                }
            }
            return res
        }
//es6
function flatten2(arr){
            while(arr.some(item=>Array.isArray(item))){
              arr=[].concat(...arr)  
            }
            return arr
}

实现深拷贝

function deepClone(arr,map=new Map()){
            if(typeof arr==='object'){
                let res=Array.isArray(arr)?[]:{}
                if(map.get(arr)){
                    return map.get(arr)
                }
                map.set(arr,res)
                for(let item in arr){
                    res[item]=deepClone(arr[item],map)
                }
                return res
            }else{
                return arr
            }
        }

发布订阅模式

class Dep{
            constructor(callback){
                this.subs=[]
                this.callback=call
            }
            addSub(sub){
                this.subs.push(sub)
            }
            notify(){
                this.subs.forEach(item=>item.update(this.callback))
            }
        }
        class pub{
            constructor(){
                this.subs=[]
            }
            addDep(sub){
                this.subs.push(sub)
            }
            publish(dep){
                this.subs.forEach(item=>item==+dep&&item.notify())
            }
        }
        class sub{
            constructor(){
                this.val=val
            }
            update(callback){
                callback(this.val)
            }
        }

URL提取参数

function querySearch(url,name){
            let tem = url.split('?')[1].split('&')
            let res= undefined
            tem.forEach(item=>{
                let {key,value}=item.split('=')
                value=decodeURIComponent(value)
                if(key === name){
                    if(res===undefined){
                        res=value
                    }else{
                        res=[].concat(res,value)
                    }
                }
            })
            return res
        }

防抖

function tem(fun,t){
            let timer
            return function(){
                clearTimeout(timer)
                let args=arguments
                timer=setTimeout(() => {
                    fun.apply(this,args)
                }, t);
            }
        }

节流

function throttle(fun,t){
            let time1=0
            return function(){
                let time2=new Date()
                if(time2-time1>t){
                    fun.apply(this,arguments)
                    time1=time2
                }
            }
        }

函数柯里化

function curry(fn){
            let judge=(...args)=>{
                if(args.length===fn.length)return fn(...args)
                return (...arg)=>judge(...args,...arg)
            }
            return judge 
        }

实现forEach

Array.prototype.forEach2 = function(callback, thisArg) {
    if (this == null) {
        throw new TypeError('this is null or not defined')
    }
    if (typeof callback !== "function") {
        throw new TypeError(callback + ' is not a function')
    }
    const O = Object(this)  // this 就是当前的数组
    const len = O.length >>> 0  // 后面有解释
    let k = 0
    while (k < len) {
        if (k in O) {
            callback.call(thisArg, O[k], k, O);
        }
        k++;
    }
}

 未完待续

posted @ 2022-08-09 18:34  北火廿口  阅读(34)  评论(0)    收藏  举报