前端常见面试题笔记

/**
 * 防抖
 * 一段时间内触发重新计时
 */

function debounce (func, delay) {
    let timer = null;
    return function(){
        if(timer) clearTimeout(timer)
        timer = setTimeout(() => {
            // 箭头函数直接继承父级作用域 所以可 ...argumnets
            func(...arguments);
        }, delay);
    }
}

/**
 * 节流
 * 一定时间内,执行第一次,后面不执行
 */

function throttle(func, delay){
    let timer = null, self = this;
    return function(){
        if(timer) return;
        timer = setTimeout(() => {
            func.apply(self, arguments);
            timer = null;
        }, delay);
    }
}
apply call 和 bind 的差别
相同点
1.改变this 指向
2.第一个参数是this要指向的对象
3.都可以利用后续参数传参

不同点
1.call,bind 参数依次传入 apply只有两个参数,第二个参数是数组
2.call, apply直接调用 bind返回一个指向this的函数

原型链
对象只有_proto_属性,没有prototype属性
for in 能迭代出原型里面的属性

箭头函数
1.箭头函数简洁, 参数只有一个, 可以省略参数的括号,函数体只有一句,可省略花括号
2.不能作为构造函数使用
3.箭头函数没有自己的this,继承上一级作用域的this, this在定义的时候已经确定了,不会再发生改变
4.call apply bind 不能改变this的指向
5.没有自己的arguments, 箭头函数中的arguments实际上是上一级作用域中的arguments
定义对象的花括号不能单独形成一个执行环境
posted @ 2023-04-06 12:47  小僵尸  阅读(13)  评论(0编辑  收藏  举报