当前位置:网站首页>节流,防抖,new函数,柯里化
节流,防抖,new函数,柯里化
2022-06-26 08:55:00 【鹏程933】
防抖
- 防抖就是固定时间内只触发一次(也就是在连续点击时,停止时间大于规定值才会触发事件)
- 通过定时器与闭包来实现
function debounce(fun,ms=1000){
let timer; // 定义开关,当它为false时,定时器启动,为true时,清除掉上一个定时器
return function (...args){
if(timer){
clearTimeout(timer)
}
timer=setTimeout(()=>{
// 启动定时器,规定的防抖时间到了后再执行
fun.call(this,args)
timer=null
},ms)
}
}
const task=()=>{
console.log('大于设置时间间隔,事件触发')
}
const debounceTask=debounce(task,1000) // 定义事件函数,规定防抖事件为1000ms
/* * 测试 * 当 * setInterval(()=>{ * console.log('一直触发事件中') * debounceTask() * },500) * 也就是触发的频率大于规定的1000ms,控制台只打印’一直触发事件中‘,不会出现’大于设置时间间隔,事件触发‘ */
setInterval(()=>{
console.log('一直触发事件中')
debounceTask()
},2000)
/* * 这个触发频率大于1000ms,每个2000ms打印’一直触发事件中‘和’大于设置时间间隔,事件触发‘ */
节流
- 节流就是每隔固定事件触发一次(也就是在连续点击时,不管你点击频率,每隔固定事件就触发一次事件)
- 也采用闭包加定时器实现
function throttle(fun,ms=1000){
let canRun=true // 定义开关,默认为true,会启动定时器,false时,在判断语句中结束掉进程
return function (...args){
if(!canRun){
return } // 当canRun为false时触发,也就是已经有一个定时器执行着,就结束该进程;也就是在此处截断
canRun=false // 将canRun的状态改为false,那么在里面定时器执行完毕之前不会再向后走,也就是阻止后面事件触发时响应
setTimeout(()=>{
fun.apply(this,args)
canRun=true // 执行完毕后,允许后面事件进来响应
},ms)
}
}
const task=()=>{
console.log('大于设置时间间隔,事件触发')
}
const throttleTask=throttle(task,1000)
/* * 测试 * 虽然执行频率为500ms一次,但控制台依然1000ms打印一次’大于设置时间间隔,事件触发‘ * 控制台打印 * 一直触发事件中 * 一直触发事件中 * 大于设置时间间隔,事件触发 * 一直触发事件中 * 一直触发事件中 * 大于设置时间间隔,事件触发 */
setInterval(()=>{
console.log('一直触发事件中')
throttleTask()
},500)
注:可以使用lodash来完成防抖与节流
new
- ES6语法糖class原理
function myNew(Func,...args){
const instance={
}
if(Func.prototype){
// 如果函数原型存在,将instance指向原型对象上去
Object.setPrototypeOf(instance,Func.prototype)
}
const res=Func.apply(instance,args) // 调用构造器,并将内部的this用instance来代替
if(typeof res==='function' || typeof res==='object' && typeof res===null){
// 兼容处理下返回值
return res
}
return instance
}
function Person(name){
this.name=name
}
Person.prototype.eat=function (){
console.log(this.name+'吃饭')
}
const p1=myNew(Person,'jakc')
p1.eat()
// jack吃饭
柯里化
- 函数柯里化就是我们给一个函数传入一部分参数,此时就会返回一个函数来接收剩余的参数,后面执行返回的那个函数
- 将函数的一个参数拆分为两个乃至更多参数
function curry(func){
return function curried(...args){
// 传入的参数大于等于原始函数func的参数个数,则直接执行该函数
if(args.length >= func.length){
return func.apply(this, args)
}
/** * 传入的参数小于原始函数fn的参数个数时 * 则继续对当前函数进行柯里化,返回一个接受所有参数(当前参数和剩余参数) 的函数 */
return function (...args2){
return curried.apply(this,args.concat(args2))
}
}
}
function sum(a,b,c){
return a+b+c
}
const curriedSum=curry(sum)
console.log(curriedSum(1,2,3))
console.log(curriedSum(1)(2,3))
console.log(curriedSum(1)(2)(3))
// 输出结果都为6
边栏推荐
- Merrill Lynch data technology expert team | building a cloud native product system based on containers
- install ompl.sh
- Runtimeerror: object has no attribute NMS error record when using detectron2
- 【CVPR 2019】Semantic Image Synthesis with Spatially-Adaptive Normalization(SPADE)
- Self taught machine learning series - 1 basic framework of machine learning
- Bbox format conversion (detectron2 function library)
- Detectron2 outputs validation loss during training
- php提取txt文本存储json数据中的域名
- "One week's work on Analog Electronics" - Basic amplification circuit
- Is it safe to dig up money and make new debts
猜你喜欢

全面解读!Golang中泛型的使用
![[pulsar learning] pulsar Architecture Principle](/img/ec/5ab9aabc2beafd4238dc8055ba6fb2.png)
[pulsar learning] pulsar Architecture Principle

xsync同步脚本的创建及使用(以Debian10集群为例)

【CVPR 2021】Unsupervised Pre-training for Person Re-identification(UPT)

The most complete and simple nanny tutorial: deep learning environment configuration anaconda+pychart+cuda+cudnn+tensorflow+pytorch

A Style-Based Generator Architecture for Generative Adversarial Networks

"One week's work on Analog Electronics" - diodes

《一周搞定模电》—基本放大电路

Learning to Generalize Unseen Domains via Memory-based Multi-Source Meta-Learning for Person Re-ID

深度学习(初识tensorflow2.版本)之三好学生成绩问题(1)
随机推荐
[pulsar learning] pulsar Architecture Principle
Vipshop work practice: Jason's deserialization application
Nacos注册表结构和海量服务注册与并发读写原理 源码分析
Thinking before QPM preparation optimization
Adding confidence threshold for demo visualization in detectron2
Nacos registry structure and the principle of massive service registration and concurrent read-write source code analysis
挖财打新债安全吗
计算领域高质量科技期刊分级目录
Principe et application du micro - ordinateur à puce unique - Aperçu
"One week's work on Analog Electronics" - Basic amplification circuit
"One week's solution to analog electricity" - power circuit
Statistics of various target quantities of annotations (XML annotation format)
The shutter tabbar listener is called twice
Principle and application of single chip microcomputer -- Overview
2021年全国职业院校技能大赛(中职组)网络安全竞赛试题(1)详细解析教程
Thinkphp5 using the composer installation plug-in prompts that the PHP version is too high
Solutions for safety management and control at the operation site
《一周搞定数电》——编码器和译码器
首期Techo Day腾讯技术开放日,628等你
【C】青蛙跳台阶和汉诺塔问题(递归)