前端js高级部分 检测变量方法+防抖节流+浏览器事件循环模型

一、检测变量类型方法

一、instanceof

1.注意事项

表达式: A instanceof  B

B函数的显式原型对象在A对象原型链上,返回true,否则返回false

num, str和bool没有检测出它的类型

2、使用instanceof还可以检测出提前被定义好的对象
console.log(
    num insatnceof Number //false
    arr instanceof Array // true
    data instanceof Data // true
    reg intanceof RegExp // true
}

二、 typeof()

*当变量是number, string, boolean, function, undefined, json类型时,可以使用typeof进行判断,而其他变量是判断不出类型的,包括null。

typeof是区分不出array和json类型的。因为使用typeof这个变量时,array和json类型输出的都是object。

 三、constructor

constructor本来是原型对象上的属性,指向构造函数。

可以看到它指向了Number的构造函数,因此,我们可以使用num.constructor==Number来判断num是不是Number类型的,其他的变量也类似。

undefined和null没有constructor属性,没有进行测试

let num = 123;
console.log(num.constructor)//Number

四、 Object.prototype.toString.call

1、所以变量的类型都检测出来了

console.log(
Object.prototype.toString.call(num)//'[Object Number]'
Object.prototype.toString.call(str)//'[Object String]'
Object.prototype.toString.call(arr)//'[Object Array]'
Object.prototype.toString.call(und)//'[Object undefined]'
}

二、防抖节流 

一、防抖

1、定义

在一段时间内触发很多次,只执行最后一次。(从新开始

2、应用场景 

搜索框输入,文本编辑器实时保存

3、模板
let timerId = null
document.querylector('.ip').onkeyup = function(){
    if(timerId !== null){
        clearTimeout(timerId)
    }
    timerId = setTimeout(()=>{
    console.log('防抖')




},1000)
}

二、节流

1、定义

连续触发一件事,但是在设定时间只执行一次函数(不要打断我

 2、应用场景

高频事件:快速点击,鼠标滑动,scroll事件,resize事件

下拉加载

视频播放记录时间

3、模板
let time = null
function(){
    if(time !== null){
        return
    time = setTimeout(()=>{

        console.log("节流")
        time = null

    },1000)

}


三、浏览器事件循环模型

1、事件循环是什么

代码:同步任务,异步任务

异步任务:微任务,宏任务

宏任务:script代码块,setTimeout,setInterval定时器....

微任务:process.nextTick(node)    Promise.then() catch()    Async/Await     Obiect.observe.....

  • 23
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值