前端面试手写篇

手写篇

1. 手写 instenceof

原生的instanceofweb

console.log([] instanceof Array) // true

console.log('' instanceof Array) // false

手写myInstanceof数组

function myInstanceof(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__
        
    }
}

console.log(myInstanceof([],Array))// true

console.log(myInstanceof('',Array))// false

实现原理:app

经过不断的沿着原型链查找,若是找到顶端了即:proto === null,那么就说明没有找到,返回false,说明 left 不是 right 构造函数的实例编辑器

若是找到隐式原型 proto等于构造函数的原型prototype,那么说明 leftright 构造函数的实例,返回true函数

其它状况就是不断的改变proto,以即可以不断的往上查找ui

2. 手写 flat

原生示例:this

const arr1 = [1, 2, [3, 4]];
arr1.flat(); 
// [1, 2, 3, 4]

const arr2 = [1, 2, [3, 4, [5, 6]]];
arr2.flat();
// [1, 2, 3, 4, [5, 6]]

const arr3 = [1, 2, [3, 4, [5, 6]]];
arr3.flat(2);
// [1, 2, 3, 4, 5, 6]

const arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
arr4.flat(Infinity);
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

手写flatDeep:spa

function flatDeep( arr, dep=1 ){
    let ret = []
    
    for(let i=0;i<arr.length;i++){
        
        if(Array.isArray(arr[i])){
            
            dep>0 ? (ret = ret.concat(flatter(arr[i],dep-1))):(ret.push(arr[i]))
            
        }else{
            
            ret.push(arr[i]) 
        }
    }
    
    return ret
}

实现原理:prototype

第一个参数是数组,第二个是降维层级,code

用for循环遍历这个数组,检测每一项

若是这项是否是数组则直接添加到ret结果数组里面

不然根据降维层级判断,默认是降一维层级,当递归降维不知足ret>0,说明已经达到dep降维层数了,其它状况即ret.push(arr[i])

3. 手写 call

Function.prototype.myCall = function(context){

    context =(context === null || context === undefined) ? window : context
    
    context.fn = this// 其实就等价于 obj.fn = function say(){} 当指向 context.fn 时,say里面的this 指向obj [关键]
    //obj 此时变成 var obj = {name:'innerName',fn:function say(){console.log(this.name)}}

    let args = [...arguments].slice(1) //截取第二个开始的全部参数
    let result= context.fn(...args)//把执行的结果赋予result变量

    delete context.fn //删除执行上下文上的属性 (还原)由var obj = {name:'innerName',fn:function say(){console.log(this.name)}}删除fn
    return result
}
var name = 'outerName'
var obj = {
    name:'innerName'
}
function say(){
    console.log(this.name)
}
say()//outerName 等价于 window.say this指向window
say.myCall(obj)//innerName

实现原理:

函数的原型方法call 第一个参数是传入的执行上下文,后面传入的都是参数,以逗号隔开

当传入的是null或undefined是执行上下文是指向window,否使为传入的对象,而后再传入的对象身上添加fn属性并把函数实例say函数赋值给fn,此时变成

var obj = {name:'innerName',fn:function say(){console.log(this.name)}}此时context就是obj对象啦,全部你执行context.fn(...args)

其实就是obj.fn(...args)fn 其值是 function say(){ console.log(this.name) },因此这个this就变成obj对象了

而后就是结果赋值,对象还原

返回结果

4. 手写 apply

Function.prototype.myApply = function(context){
    
    context =(context === null || context === undefined) ? window : context
    
    let result
    
    context.fn = this
    
    result = arguments[1] ? context.fn(...arguments[1]) : context.fn()
    
    delete context.fn
    
    return result
}

myCall实现原理大体相同,不一样的是因为callapply的传参方式不同,

咱们须要额外的对第二个参数作判断,apply受参形式是数组,且再第二个参数位置,

一:若是第二个参数存在,执行的时候就把第二个参数(数组形式)用扩展运算符打散后传入执行

二:若是第二个参数不存在,执行执行

其它就于call的实现同样

5. 手写 bind

Function.prototype.myBind = function(context){
    
    context =(context === null || context === undefined) ? window : context
    
    let o = Object.create(context)
    
    o.fn = this
    
    let args = [...arguments].slice(1)
    
    let fn= function(){
        
        o.fn(...args)
    }
    
    return fn
}

bind 的手写实现,与其它两个区别是返回一个函数,并没返回函数执行的结果,而且受参形式不受限制

实现原理:

经过 Object.create方法建立一个新对象,使用现有的对象来提供新建立的对象的__proto__,经过 中介对象o来实现,来达到不影响传入的对象

6. 手写 new

new 一个函数的时候,会生成一个实例,该实例的隐式原型__proto__ ===该函数的prototype原型对象

在构造函数中this指向当前实例

最后再将实例对象返回

function myNew(func){
    
    //第一步 将函数的 prototype 指向 o 对象的__proto__
    let o = Object.create(func.prototype)
    
    //第二步 经过call改变 this的指向,使之指向 o
    let ret = func.call(o)
    
    //第三步 若是构造函数里面有返回对象,则返回这个对象,没有则返回 o 对象
    return typeof ret === 'object' ? ret : o

}

检测:

function M(){}

let m = myNew(M); // 等价于 new M 这里只是模拟
console.log(m instanceof M); // instanceof 检测实例
console.log(m instanceof Object);
console.log(m.__proto__.constructor === M);
相关文章
相关标签/搜索