前端面试(手写题)

必背面试题web前端面试题(必背面试题)_Z_Xshan的博客-CSDN博客_web前端面试题

面试官:手写防抖与节流

//防抖 
function debounce(func,wait=5000){
            let timer=0;
           return function(...args){

                if (timer) clearTimeout(timer)
                timer=setTimeout(()=>{
                    console.log(2);
                    func.apply(this,args)
                },wait)
            }
        }

//节流
function throttle(func,delay=2000){
    var timer=1;
    return function(...args){
        if(timer){
            timer=setTimeout(()=>{
                func.apply(this,args)
                timer=0
            },delay)
        }
    }
}

面试题:实现instanceof

function _instanceof(example,classFunc){
    if (typeof example!=='object' || example==null) return false
    let proto=Object.getPrototypeOf(example);
    
    while(true){
        if (proto==classFunc.prototype) return true;
        proto=Object.getPrototypeOf(proto)
    }

}
console.log(_instanceof([], Array));  //true

面试题:实现new方法

function myNew(fn, ...args){
        // 基于原型链 创建一个新对象
        let newObj=Object.create(fn.prototype);
        // 添加属性到新对象上 并获取obj函数的结果
        let res=fn.apply(newObj,args)
        // 如果执行结构有返回值并是一个对象,返回执行结果,否则,返回新创建的对象
        return typeof res==='object'?res:newObj;
}
// 用法
function Person(name,age){
    this.name=name;
    this.age=age;
}
Person.prototype.say=function(){
    console.log(this.age);
}
let p1=myNew(Person,'poety',18);
console.log(p1.name);
console.log(p1);
p1.say

面试题:实现深拷贝

let obj = {
            val: [12, 4, 'pp'],
            name: 'name',
            age: 12,
            obj: {
                name: 'n',
                age: 3
            },
        }
 function deepClone(source) {
            let tarGetobj = source.constructor === Array ? [] : {};
            for (let keys in source) { //如果是数组循环索引 对象循环键名
                if (source[keys] && source[keys] instanceof Object) {
                    tarGetobj[keys] = deepClone(source[keys])
                } else {
                    tarGetobj[keys] = source[keys]
                }
            }

            return tarGetobj
        }

let new obj = deepClone(obj);

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Z_Xshan

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值