这些手写函数你会写了吗

133 阅读2分钟

一、call

//call
Function.prototype.mycall=function(context){
    var context=context?Object(context):window
    context.fn=this
    var args=[]
    for(var i=1;i<arguments.length;i++){
        args.push('arguments['+i+']')
    }
    var result=eval('context.fn('+args+')')
    delete context.fn//别忘了删
    return result
}

二、apply

//apply
Function.prototype.myapply=function(context,args){
    var context=context?Object(context):window
    context.fn=this
    if(!args){//没有的话直接返回执行结果
        return context.fn()
    }
    var result=eval('context.fn('+args+')')
    delete context.fn
    return result
}

三、bind

//bind
Function.prototype.mybind=function(context){
    var that=this
    var firstArgs=Array.prototype.slice.call(arguments,1)

    function Fn(){}
    var returnFunction=function(params){
        var args=Array.prototype.slice.call(arguments)
        var finallyArgs=firstArgs.concat(args)
        return that.apply(this instanceof returnFunction?this:context,finallyArgs)
    }
    Fn.prototype=this.prototype
    returnFunction.prototype=new Fn()
    return returnFunction
}

缺四、promise

五、new

var mynew=function(){
    var constructor=Array.prototype.shift.call(arguments)
    var obj=new Object()
    obj.__proto__=constructor.prototype
    var result=constructor.apply(obj,arguments)
    return result instanceof Object?result:obj
}

六、instanceOf

var myInstanceOf=function(left,right){
    var rigth=right.prototype
    var left=left.__proto__
    while(true){
        if(left==null){
            return false
        }
        if(left==right){
            return true
        }
        left=left.__proto__
    }
}

七、object.create()

创建的对象的原型对象是现有对象

Object.create 创建一个新的对象,将现有对象作为新对象的__proto__. 参数1:作为__proto__的对象 参数2: 类型为Object,可以为新对象添加属性

let obj = {
     name: 'sss',
     age: 12
 }
const me = Object.create(obj); //返回一个新的空对象 ,并将现有对象作为新对象__proto__
console.log(me , me.__proto__)//{} ,  {name: "sss", age: 12}
console.log(me.age) //12
var myCreate=function(obj){
    var Fn=function(){}
    Fn.prototype=obj
    return new Fn()
}

八、原生的ajax

九、手写防抖和节流

十、jsonp

十一、手写深浅拷贝

十二、函数柯里化

十三、js去重、合并

1、判断是否是数组

var arr=[]
//1
console.log(arr instanceof Array)
//2
console.log(arr.constructor===Array)
//3、最优
console.log(Array.isArray(arr))

2、数组合并

var arr= [1,2,3,4]
var arr2= [1,2,3,4]
console.log(arr.concat(arr2))
//ES6
console.log([...arr,...arr2])

3、数组去重复

 var arr = [2, 8, 5, 0, 5, 2, 6, 7, 2]
    var newArr = []
    for (var i = 0; i < arr.length; i++) {
      if (newArr.indexOf(arr[i]) === -1) {//如果要检索的字符串值没有出现,则该方法返回 -1。
        newArr.push(arr[i])
     }
    }
    console.log(newArr) // 结果:[2, 8, 5, 0, 6, 7]

//  var arr = [2, 8, 5, 0, 5, 2, 6, 7, 2]
   arr.sort()
   var newArr = [arr[0]]
   for (var i = 1; i < arr.length; i++) {
     if (arr[i] !== newArr[newArr.length - 1]) {
       newArr.push(arr[i])
     }
   }
   console.log(newArr) // 结果:[0, 2, 5, 6, 7, 8]

十四、数组扁平化

function flatten(arr){
    var result=[]
    for(var i=0;i<arr.length;i++){
        if(Array.isArray(arr[i])){
            result=result.concat(flatten[arr[i]])
        }else{
            result=result.concat(arr[i])
        }
    }
    return result
}

参考链接:

1、github.com/mqyqingfeng…

2、juejin.cn/post/685641…