防抖节流与this指向的改变

一、防抖节流

1、防抖

在固定的时间内没有触发事件,会在固定时间结束后触发,如果固定时间内触发事件了,会在延长固定时间再触发
防抖主要利用定时器实现
用实例的话,可以用电梯的升降,遇到人进入会有一段时间的停止,当再次有人进入延迟会延长。


//用定时器实现防抖
function debounce(func,wait) {
    var timer=null;
    return function() {
    //保存当前调用的dom对象
     var _this=this;
     //保存事件对象
     var args=arguments;
     clearTimeout(timer)
     timer=setTimeout(function() {
         func.apply(_this,args)
     },wait)
    }

}

2、节流

无论在固定时间内是否有事件触发,都会按照固定时间规律触发
节流的话,用实例讲的话用王者荣耀里的技能键来讲,比较通俗易懂😊,在规定的时间你执行,在此期间点击,不进行触发!

具体实现有两种方法

第一种:时间戳
//时间戳版本实现节流
function throttle(func,wait) {
    //定义初始时间
    var oldTime=0;

    return function() {
        var _this=this;
        var args=arguments;

        //当前时间戳
        var newTime=+new Date();

        //判断用当前时间减去旧的时间,如果大于wait指定的时间就会触发
        if(newTime-oldTime>wait) {
            //执行触发的函数
            func.apply(_this,args)
            //将旧时间更新
            oldTime=newTime;
        }

    }

第二种:定时器
//时间戳版本实现节流
function throttle(func,wait) {
    var timer=null;

    return function() {
        var _this=this;
        var args=arguments;
       if(!timer) {
            timer=setTimeout(function() {
                timer=null;
                func.apply(_this,args)
            },wait)
       }
    }
}

目前市面主流工具函数库:lodash已经提供了全面的工具方法

npm install lodash

import _ from ‘lodash’

二、this指向的改变

call,apply,bind都是用于改变this指向的

区别:

传参不同

call用逗号分隔的形式传参
函数名.call(目标对象,参数1,参数2,…参数n)

例如:getName.call(obj,‘王武’,25,‘北京’)
apply参数用数组的形式传递
函数名.apply(目标对象,[参数1,参数2,…参数n])
例如:getName.apply(obj,[‘王g11’,25,‘上海’])
bind用逗号形式传参
getName.bind(obj,‘好11’,25,‘上海’)()

getName.bind(obj)(‘剑1’,25,‘上海’)
函数是否被执行

cal和apply是直接调用函数
bind是返回函数本身,如果要执行,必须再后面再加()调用

getName.bind(obj)()
call,apply实现原理

call的实现原理(不用call,自己手动模拟实现一个call的功能)
call是基于函数实现的
给作用的目标对象添加一个临时函数,处理赋值操作
接收参数处理
最后再删除这个临时函数
实例化对象=new 构造函数() //其中构造函数也称为类,一个类可以生成多个实例化对象

var f1=new Function() // 其中的构造函数中this=f1 永远相等
var p1=new Person() //其中的构造函数中this
=p1 永远相等
//call模拟实现原理代码:
Function.prototype.call2 = function (context) {
//目标对象
context = context || window;

//this===实例化的函数,函数本质上也是对象

//给context添加一个临时函数
context.fn = this;

//接收参数处理  arguments
console.log('arguments:',arguments)
var args = [];
for (var i = 1; i < arguments.length; i++) {

   // ["arguments[0]", "arguments[1]", "arguments[2]"]
    args.push('arguments['+i+']')
   // args.push(arguments[i])
}

 //传参执行context.fn()函数
 eval('context.fn(' + args + ')')


//删除临时函数
delete context.fn

}

apply实现原理
Function.prototype.apply2 = function (context,arr) {
//目标对象
context = context || window;

//this===实例化的函数,函数本质上也是对象

//给context添加一个临时函数
context.fn = this;

if (!arr) {
    context.fn()
} else {
    //接收参数处理  arguments
    var args = [];
    for (var i = 0; i < arr.length; i++) {

    // ["arguments[0]", "arguments[1]", "arguments[2]"]
        args.push('arr['+i+']')
    // args.push(arguments[i])
    }

    //传参执行context.fn()函数
    eval('context.fn(' + args + ')')


 }

//删除临时函数
delete context.fn

}

bind实现原理

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue中,有一些常用的防抖节流插件可以方便地实现防抖节流的功能。以下是两个常用的插件: 1. Lodash(防抖节流) Lodash是一个JavaScript实用工具库,它提供了许多常用的函数和方法,包括防抖节流函数。使用Lodash的`debounce`和`throttle`函数可以很方便地实现防抖节流。 安装Lodash: ```bash npm install lodash ``` 使用示例: ```javascript import { debounce, throttle } from 'lodash'; // 防抖示例 const debouncedFunc = debounce(() => { console.log('执行操作'); }, 500); // 节流示例 const throttledFunc = throttle(() => { console.log('执行操作'); }, 200); ``` 2. Vue-lodash(防抖节流) Vue-lodash是一个专门为Vue开发的Lodash插件,它提供了Vue指令的方式来使用Lodash的方法,包括防抖节流。 安装Vue-lodash: ```bash npm install vue-lodash ``` 在Vue项目中使用Vue-lodash示例: ```javascript import Vue from 'vue'; import VueLodash from 'vue-lodash'; import { debounce, throttle } from 'lodash'; Vue.use(VueLodash, { lodash: { debounce, throttle } }); ``` 在Vue组件中使用防抖节流: ```html <template> <div> <button v-debounce:click="debouncedFunc">点击按钮(防抖)</button> <button v-throttle:click="throttledFunc">点击按钮(节流)</button> </div> </template> <script> export default { methods: { debouncedFunc() { console.log('执行操作'); }, throttledFunc() { console.log('执行操作'); }, }, }; </script> ``` 以上是两个常用的Vue插件,可以方便地在Vue项目中使用防抖节流功能。根据具体需求选择合适的插件来使用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值